cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cn.ksource.core.util;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class BwlUtil {
 
    public static List<Map> doFilter(List<Map> list,String primaryKey){
        List<Map> resultList = new ArrayList<Map>();
        Map<String,Object> bwlMap = new HashMap();
        
        for (Map map : list) {
            if (!bwlMap.containsKey(map.get(primaryKey))) {
                Map tempMap = new HashMap();
//                for (Object obj : map.keySet()) {
//                    tempMap.put(ConvertUtil.obj2Str(obj), map.get(obj));
//                }
                tempMap.putAll(map);
                List<Map> tempList = new ArrayList<Map>();
                tempList.add(map);
                tempMap.put("DATALIST", tempList);
                resultList.add(tempMap);
                bwlMap.put(ConvertUtil.obj2Str(map.get(primaryKey)), tempMap);
            } else {
                List<Map> tempList = (List<Map>)((Map)bwlMap.get(map.get(primaryKey))).get("DATALIST");
                tempList.add(map);
            }
        }
        return resultList;
    }
    public static List<Map> doFilter(List<Map> list,String primaryKey,String subListNames){
        List<Map> resultList = new ArrayList<Map>();
        Map<String,Object> bwlMap = new HashMap();
        for (Map map : list) {
            if (!bwlMap.containsKey(map.get(primaryKey))) {
                Map tempMap = new HashMap();
                for (Object obj : map.keySet()) {
                    tempMap.put(ConvertUtil.obj2Str(obj), map.get(obj));
                }
                List<Map> tempList = new ArrayList<Map>();
                tempList.add(map);
                tempMap.put(subListNames, tempList);
                resultList.add(tempMap);
                bwlMap.put(ConvertUtil.obj2Str(map.get(primaryKey)), tempMap);
            } else {
                List<Map> tempList = (List<Map>)((Map)bwlMap.get(map.get(primaryKey))).get(subListNames);
                tempList.add(map);
            }
        }
        return resultList;
    }
    /**
     * 功能描述:备忘录算法<br>
     * 
     * @return List<Map<String,Object>> 作者:杨凯<BR>
     *         时间:Aug 27, 2008 <br>
     */
    public static List<Map<String, Object>> bwl(List list, String[][] keys, String[] subListNames) {
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
 
        // 根据备忘录的层数声明备忘录
        Map[] bwlArrays = new HashMap[keys.length];
        for (int i = 0; i < bwlArrays.length; i++) {
            bwlArrays[i] = new HashMap<String, Map>();
        }
 
        if (list != null && list.size() > 0) {
            for (Map tempMap : (List<Map>) list) {
                for (int i = 0; i < keys.length; i++) {
                    String keyValue = ConvertUtil.obj2Str(tempMap.get(keys[i][0]));
                    if (!bwlArrays[i].containsKey(keyValue)) {
                        Map<String, Object> subMap = new HashMap<String, Object>();
                        for (int j = 0; j < keys[i].length; j++) {
                            subMap.put(keys[i][j], ConvertUtil.obj2Str(tempMap.get(keys[i][j])));
                        }
                        if (i == 0) {
                            subMap.put(subListNames[i], new ArrayList());
                            bwlArrays[i].put(keyValue, subMap);
                            resultList.add(subMap);
                        } else {
                            List subList = (List) ((Map) bwlArrays[i - 1].get(tempMap.get(keys[i - 1][0]))).get(subListNames[i - 1]);
                            subList.add(subMap);
                            subMap.put(subListNames[i], new ArrayList());
                            bwlArrays[i].put(keyValue, subMap);
                        }
                    }
                }
                List subSubList = (List) ((Map) bwlArrays[bwlArrays.length - 1].get(tempMap.get(keys[keys.length - 1][0]))).get(subListNames[subListNames.length - 1]);
                subSubList.add(tempMap);
                for (int i = 0; i < keys.length; i++) {
                    for (int j = 0; j < keys[i].length; j++) {
                        tempMap.remove(keys[i][j]);
                    }
                }
            }
        }
        return resultList;
    }
}