Tools used
1. Serialization operation
String multiMapStr = (multiMapList); // Convert list to json stringObject o = (userList); // Convert list to objectList<Map<String, String>> maps = ((userList), , );// object conversion listList tzAreas = (res, ); // Deserialization,jsonConvert string tolist
2. Convert object to a map of the specified type or other type
Use the following tool class to convert object to list "map"
package ; import ; import ; import ; import ; /** * @BelongsProject: spring-boot-self-exercise * @BelongsPackage: * @Author: pingmingbo * @CreateTime: 2023-12-09 15:34 * @Description: Cache Tool Class * @Version: 1.0 */ public class CastBeanUtil { private CastBeanUtil() { } /** * Return object needs to be converted, this method can be converted to string type or other reference type * Problem: Double-layer for loop * @param obj Source data * @param kCalzz key corresponding to class type * @param vCalzz value corresponding to class type * @param <K>key type * @param <V>value type * @return list<map> */ public static <K, V> List<Map<K, V>> castListMap(Object obj, Class<K> kCalzz, Class<V> vCalzz) { List<Map<K, V>> result = new ArrayList<>(); if (obj instanceof List<?>) { for (Object mapObj : (List<?>) obj) { if (mapObj instanceof Map<?, ?>) { Map<K, V> map = new HashMap<>(16); for (<?, ?> entry : ((Map<?, ?>) mapObj).entrySet()) { ((()), (())); } (map); } } return result; } return null; } }
Processing by string type
list map form
key: string
val:Serialized string
List<Map<String, String>> multiMapList = (); for (int i = 0; i < 5; i++) { LinkedHashMap<String, String> itemMap = (); ("name", "jack" + i); if (i % 2 == 0) { ("age", (10 + i)); ("sex", "male"); } else { ("age", (11 + i)); ("sex", "female"); } (itemMap); } String multiMapStr = (multiMapList); ().set("str:multiusers", multiMapStr, DateConstant.TIME_OF_DAY, ); String userList = ().get("str:multiusers"); List<Map<String, String>> maps = ((userList), , ); ("maps : [{}]", maps);
list "entity" structure data
List<TzArea> areaList = (); for (int i = 0; i < 10; i++) { TzArea item = new TzArea(); ((long) i); ("Jiangsu Province"); (1); (1L); (item); } ().set("str:multiareas", (areaList), DateConstant.TIME_OF_DAY, ); String res = ().get("str:multiareas"); List<TzArea> tzAreas = (res, ); ("tzAreas : [{}]", (tzAreas));
This is the article about the processing of storing list<map> and list<entity> in redis. For more related redis list<map> list<entity>, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!