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>(); for (Map<String, Object> tmp : list) { setTmp.add(tmp.get(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) tmp.get(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.add(tmp.get(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) tmp.get(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); } }
//根据设备名称进行分组 Map<String, List<ModuleStatisticDetail>> collect = moduleStatisticDetails.stream() .collect(Collectors.groupingBy(ModuleStatisticDetail::getDeviceTypeName)); 复制代码 /** * 使用jdk1.8 lamada表达式排序 倒序排列 */ list=list.stream().sorted(Comparator.comparing(SortBean::getT1).reversed()).collect(Collectors.toList()); for (SortBean sortBean : list) { System.out.println(sortBean); } 复制代码