Java8 ListMap sorted() 排序
学习了List<Bean>排序,如果类型是List<Map>类型,处理上有什么区别吗:
最大的区别在于倒序,List<Bean>可以直接使用reversed()方法了,List<Map>要写下 Comparator.comparing具体的内容
代码;
数据模拟
public static List<Map<String, Object>> setCpuRateValue() {
??? Map<String, Object> paas = new HashMap<>(8);
??? List<Map<String, Object>> neIdList = new ArrayList<>();
??? paas.put("metric", "400000279");
??? paas.put("neId", "1000000058448");
??? paas.put("value", 0.92);
??? neIdList.add(paas);
??? Map<String, Object> iaas = new HashMap<>(8);
??? iaas.put("metric", "400000273");
??? iaas.put("neId", "1000000058449");
??? iaas.put("value", 0.45);
??? neIdList.add(iaas);
??? Map<String, Object> scenes = new HashMap<>(8);
??? scenes.put("metric", "400000276");
??? scenes.put("neId", "1000000058450");
??? scenes.put("value", 0.98);
??? neIdList.add(scenes);
??? Map<String, Object> crm = new HashMap<>(8);
??? crm.put("metric", "400000279");
??? crm.put("neId", "1000000058451");
??? crm.put("value", 0.36);
??? neIdList.add(crm);
??? return neIdList;
}
public static List<Map<String, Object>> setCpuRateNullValue() {
??? Map<String, Object> paas = new HashMap<>(8);
??? List<Map<String, Object>> neIdList = new ArrayList<>();
??? paas.put("metric", "400000279");
??? paas.put("neId", "1000000058448");
??? paas.put("value", 0.92);
??? neIdList.add(paas);
??? Map<String, Object> iaas = new HashMap<>(8);
??? iaas.put("metric", "400000273");
??? iaas.put("neId", "1000000058449");
??? iaas.put("value", 0.45);
??? neIdList.add(iaas);
??? Map<String, Object> scenes = new HashMap<>(8);
??? scenes.put("metric", "400000276");
??? scenes.put("neId", "1000000058450");
??? scenes.put("value", null);
??? neIdList.add(scenes);
??? Map<String, Object> crm = new HashMap<>(8);
??? crm.put("metric", "400000279");
??? crm.put("neId", "1000000058451");
??? crm.put("value", null);
??? neIdList.add(crm);
??? return neIdList;
}
常见排序
public static void main(String args[]) throws IOException, ClassNotFoundException {
??? testSorted();
}
public static void testSorted() {
??? List<Map<String, Object>> cpuRateList = setCpuRateValue();
??? List<Map<String, Object>> valueList = cpuRateList.stream()
??????????? .sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
??? System.out.println("============排序 ===========");
??? valueList.forEach(System.out::println);
??? // 倒序
??? //??????? Collections.reverse(valueList);
??? List<Map<String, Object>> reverseValueList = cpuRateList.stream()
??????????? .sorted((c1, c2) -> MapUtils.getDouble(c2, "value").compareTo(MapUtils.getDouble(c1, "value"))).collect(Collectors.toList());
??? System.out.println("============倒序===========");
??? reverseValueList.forEach(System.out::println);
}
处理null的情况
如果排序中,值为null了,要怎么处理呢?
public static void main(String args[]) throws IOException, ClassNotFoundException { ????testMapWithNullSorted();
}
?
public static void testMapWithNullSorted() {
??? List<Map<String, Object>> cpuRateList = setCpuRateNullValue();
??? List<Map<String, Object>> filterValueList = cpuRateList.stream().filter(e -> MapUtils.getDouble(e, "value") != null)
??????????? .sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
??? System.out.println("==========过滤null===========");
??? filterValueList.forEach(System.out::println);
??? List<Map<String, Object>> valueNullFirstList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsFirst(Double::compareTo))).collect(Collectors.toList());
??? System.out.println("==========null排前面===========");
??? valueNullFirstList.forEach(System.out::println);
??? List<Map<String, Object>> valueNullLastList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsLast(Double::compareTo))).collect(Collectors.toList());
??? System.out.println("==========null排后面===========");
??? valueNullLastList.forEach(System.out::println);
}
JSONArray的排序同理。
JSONArray排序
public static void main(String args[]){
??? testSorted();
??? testJsonWithNullSorted();
}
public static void testSorted() {
??? JSONArray fieldList = initFiledValue();
??? List<JSONObject> sortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
??????????? .sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
??? System.out.println("============排序 ===========");
??? sortIdList.forEach(System.out::println);
??? // 倒序
??? List<JSONObject> reverseSortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
??????????? .sorted((c1, c2) -> MapUtils.getInteger(c2, "sortId").compareTo(MapUtils.getInteger(c1, "sortId"))).collect(Collectors.toList());
??? System.out.println("============倒序===========");
??? reverseSortIdList.forEach(System.out::println);
}
public static void testJsonWithNullSorted() {
??? JSONArray fieldList = initFiledNullValue();
??? List<JSONObject> filterValueList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).filter(e -> MapUtils.getInteger(e, "sortId") != null)
??????????? .sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
??? System.out.println("==========过滤null===========");
??? filterValueList.forEach(System.out::println);
??? List<JSONObject> valueNullFirstList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsFirst(Integer::compareTo))).collect(Collectors.toList());
??? System.out.println("==========null排前面===========");
??? valueNullFirstList.forEach(System.out::println);
??? List<JSONObject> valueNullLastList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
??? System.out.println("==========null排后面===========");
??? valueNullLastList.forEach(System.out::println);
}
//
public static JSONArray initFiledValue() {
??? String data = "[{\n" +
??????????? "\t\"fieldName\": \"model_name\",\n" +
??????????? "\t\"sortId\": 2,\n" +
??????????? "\t\"elementLabel\": \"模型名\"\n" +
??????????? "}, {\n" +
??????????? "\t\"elementName\": \"model_mod\",\n" +
??????????? "\t\"sortId\": 1,\n" +
??????????? "\t\"elementLabel\": \"模型ID\"\n" +
??????????? "}, {\n" +
??????????? "\t\"fieldName\": \"remark\", \n" +
??????????? "\t\"sortId\": 3,\n" +
??????????? "\t\"elementLabel\": \"备注\"\n" +
??????????? "}, {\n" +
??????????? "\t\"fieldName\": \"model_type\", \n" +
??????????? "\t\"sortId\": 4,\n" +
??????????? "\t\"elementLabel\": \"类型\" \n" +
??????????? "}]";
??? return JSONArray.parseArray(data);
}
public static JSONArray initFiledNullValue() {
??? String data = "[{\n" +
??????????? "\t\"fieldName\": \"model_name\",\n" +
??????????? "\t\"sortId\": 2,\n" +
??????????? "\t\"elementLabel\": \"模型名\"\n" +
??????????? "}, {\n" +
??????????? "\t\"elementName\": \"model_mod\",\n" +
??????????? "\t\"sortId\": null,\n" +
??????????? "\t\"elementLabel\": \"模型ID\"\n" +
??????????? "}, {\n" +
??????????? "\t\"fieldName\": \"remark\", \n" +
??????????? "\t\"sortId\": 3,\n" +
??????????? "\t\"elementLabel\": \"备注\"\n" +
? ??????????"}, {\n" +
??????????? "\t\"fieldName\": \"model_type\", \n" +
??????????? "\t\"sortId\": 4,\n" +
??????????? "\t\"elementLabel\": \"类型\" \n" +
??????????? "}]";
??? return JSONArray.parseArray(data);
}
总结:
??? ?List<Map>在处理倒序的时候,要写下Comparator.comparing,其它的使用跟List<Bean>差不多。排好序,就可以进行下一步操作。
|