SoFunction
Updated on 2025-03-04

Processes to store list<map> and list<entity> in redis

Tools used

1. Serialization operation

String multiMapStr = (multiMapList); // Convert list to json stringObject o = (userList); // Convert list to objectList&lt;Map&lt;String, String&gt;&gt; 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 &lt;K, V&gt; List&lt;Map&lt;K, V&gt;&gt; castListMap(Object obj, Class&lt;K&gt; kCalzz, Class&lt;V&gt; vCalzz) {
        List&lt;Map&lt;K, V&gt;&gt; result = new ArrayList&lt;&gt;();
        if (obj instanceof List&lt;?&gt;) {
            for (Object mapObj : (List&lt;?&gt;) obj) {
                if (mapObj instanceof Map&lt;?, ?&gt;) {
                    Map&lt;K, V&gt; map = new HashMap&lt;&gt;(16);
                    for (&lt;?, ?&gt; entry : ((Map&lt;?, ?&gt;) mapObj).entrySet()) {
                        ((()), (()));
                    }
                    (map);
                }
            }
            return result;
        }
        return null;
    }

}

Processing by string type

list map form
key: string
val:Serialized string

List&lt;Map&lt;String, String&gt;&gt; multiMapList = ();
for (int i = 0; i &lt; 5; i++) {
     LinkedHashMap&lt;String, String&gt; 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&lt;Map&lt;String, String&gt;&gt; maps = ((userList), , );
("maps : [{}]", maps);

list "entity" structure data

List&lt;TzArea&gt; areaList = ();
 for (int i = 0; i &lt; 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&lt;TzArea&gt; 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!