java中对list集合中的数据按照某⼀个属性进⾏分组import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ListSort {
/**
* 按照List<Map<String,Object>>⾥⾯map的某个value重新封装成多个不同的list, 原始数据类型List<Map
* <String,Object>>, 转换后数据类型Map<String,List<Map<String,Object>>>
*
* @param list
* @param oneMapKey
* @return
*/
private static Map<String, Object> change(List<Map<String, Object>> list, String oneMapKey) {
Map<String, Object> resultMap = new HashMap<String, Object>();
Set<Object> setTmp = new HashSet<Object>();
sortedlistfor (Map<String, Object> tmp : list) {
setTmp.(oneMapKey));
}
Iterator<Object> it = setTmp.iterator();
while (it.hasNext()) {
String oneSetTmpStr = (String) it.next();
List<Map<String, Object>> oneSetTmpList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> tmp : list) {
String oneMapValueStr = (String) (oneMapKey);
if (oneMapValueStr.equals(oneSetTmpStr)) {
oneSetTmpList.add(tmp);
}
}
resultMap.put(oneSetTmpStr, oneSetTmpList);
}
return resultMap;
}
/**
* 按照List<Map<String,Object>>⾥⾯map的某个value重新封装成多个不同的list, 原始数据类型List<Map
* <String,Object>>, 转换后数据类型Map<String,List<Map<String,Object>>>
*
* @param list
* @param oneMapKey
* @return
*/
private static List<Map<String, Object>> change2(List<Map<String, Object>> inList, String oneMapKey,
List<Map<String, Object>> outList) {
// 1.将某个key的值存在set中
Set<Object> setTmp = new HashSet<Object>();
for (Map<String, Object> tmp : inList) {
setTmp.(oneMapKey));
}
// 2.遍历set
Iterator<Object> it = setTmp.iterator();
while (it.hasNext()) {
String oneMapValueStr = "";
String oneSetTmpStr = (String) it.next();
Map<String, Object> oneSetTmpMap = new HashMap<String, Object>();
List<Map<String, Object>> oneSetTmpList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> tmp : inList) {
oneMapValueStr = (String) (oneMapKey);
if (oneSetTmpStr.equals(oneMapValueStr)) {
oneSetTmpMap.put("text", oneSetTmpStr);
oneSetTmpList.add(tmp);
}
}
oneSetTmpMap.put("array", oneSetTmpList);
outList.add(oneSetTmpMap);
}
return outList;
}
public static void main(String[] args) {
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
Map<String, Object> map4 = new HashMap<String, Object>();
List<Map<String, Object>> inList = new ArrayList<Map<String, Object>>();
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<String, Object> resultMap = new HashMap<String, Object>();
// resultMap = change(list, "sex");
resultMap = change(inList, "name");
System.out.println("按照map中的某个值重新封装后的resultMap:" + resultMap);
List<Map<String, Object>> outList = new ArrayList<Map<String, Object>>();
System.out.println("retList==1==:" + outList);
outList = change2(inList, "name", outList);
System.out.println("retList==2==:" + outList);
}
}
————————————————
版权声明:本⽂为CSDN博主「FOCUS_ON_AI」的原创⽂章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原⽂出处链接及本声明。原⽂链接:blog.csdn/qq_32649581/article/details/82851701
//根据设备名称进⾏分组
Map<String, List<ModuleStatisticDetail>> collect = moduleStatisticDetails.stream()
.upingBy(ModuleStatisticDetail::getDeviceTypeName));
/**
* 使⽤jdk1.8 lamada表达式排序倒序排列
*/
list=list.stream().sorted(Comparatorparing(SortBean::getT1).reversed()).List());
for (SortBean sortBean : list) {
System.out.println(sortBean);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论