importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Set;public classListSort {/*** 按照List>里面map的某个value重新封装成多个不同的list, 原始数据类型List
* >, 转换后数据类型Map>>
*
*@paramlist
*@paramoneMapKey
*@return*/private static Map change(List>list, String oneMapKey) {
Map resultMap = new HashMap();
Set setTmp = new HashSet();for (Maptmp : list) {
setTmp.add(tmp.get(oneMapKey));
}
Iterator it =setTmp.iterator();while(it.hasNext()) {
String oneSetTmpStr=(String) it.next();
List> oneSetTmpList = new ArrayList>();for (Maptmp : list) {
String oneMapValueStr=(String) tmp.get(oneMapKey);if(oneMapValueStr.equals(oneSetTmpStr)) {
oneSetTmpList.add(tmp);
}
}
resultMap.put(oneSetTmpStr, oneSetTmpList);
}returnresultMap;
}/*** 按照List>里面map的某个value重新封装成多个不同的list, 原始数据类型List
* >, 转换后数据类型Map>>
*
*@paramlist
*@paramoneMapKey
*@return*/private static List> change2(List>inList, String oneMapKey,
List>outList) {//1.将某个key的值存在set中
Set setTmp = new HashSet();for (Maptmp : inList) {
setTmp.add(tmp.get(oneMapKey));
}//2.遍历set
Iterator it =setTmp.iterator();while(it.hasNext()) {
String oneMapValueStr= "";
String oneSetTmpStr=(String) it.next();
Map oneSetTmpMap = new HashMap();
List> oneSetTmpList = new ArrayList>();for (Maptmp : inList) {
oneMapValueStr=(String) tmp.get(oneMapKey);if(oneSetTmpStr.equals(oneMapValueStr)) {
oneSetTmpMap.put("text", oneSetTmpStr);
oneSetTmpList.add(tmp);
}
}
oneSetTmpMap.put("array", oneSetTmpList);
outList.add(oneSetTmpMap);
}returnoutList;
}public static voidmain(String[] args) {
Map map1 = new HashMap();
Map map2 = new HashMap();
Map map3 = new HashMap();
Map map4 = new HashMap();
List> inList = new ArrayList>();
map1.put("id", "111");
map1.put("name", "tom");
map1.put("sex", "male");
map2.put("id", "222");
map2.put("name", "jack");
map2.put("sex", "male");
map3.put("id", "333");
map3.put("name", "lucy");
map3.put("sex", "female");
map4.put("id", "444");
map4.put("name", "lucy");
map4.put("sex", "female");
inList.add(map1);
inList.add(map2);
inList.add(map3);
inList.add(map4);
System.out.println("原始inList:" +inList);
Map resultMap = new HashMap();//resultMap = change(list, "sex");
resultMap = change(inList, "name");
System.out.println("按照map中的某个值重新封装后的resultMap:" +resultMap);
List> outList = new ArrayList>();
System.out.println("retList==1==:" +outList);
outList= change2(inList, "name", outList);
System.out.println("retList==2==:" +outList);
}
}
————————————————
版权声明:本文为CSDN博主「FOCUS_ON_AI」的原创文章,遵循 CC4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_32649581/article/details/82851701