List<Map<String,String>> arrayList = new ArrayList<>();
Map<String, String> securityMap1 = new HashMap<>();
securityMap1.put("name","期货");
securityMap1.put("value","100.11");
arrayList.add(securityMap1);
Map<String, String> securityMap2 = new HashMap<>();
securityMap2.put("name","基金");
securityMap2.put("value","200.22");
arrayList.add(securityMap2);
Map<String, String> securityMap3 = new HashMap<>();
securityMap3.put("name","股票");
securityMap3.put("value","300.33");
arrayList.add(securityMap3);
Map<String, String> securityMap4 = new HashMap<>();
securityMap4.put("name","债券");
securityMap4.put("value","400.44");
arrayList.add(securityMap4);
List<String> sortList = Arrays.asList("股票", "债券", "基金", "期货");
List<Map<String, String>> result = arrayList.stream().sorted(Comparator.comparing(e -> e.get("name"), (x, y) -> {
//按照读取的list顺序排序
for (String sort : sortList) {
if (sort.equals(x) || sort.equals(y)) {
if (x.equals(y)) {
return 0;
} else if (sort.equals(x)) {
return -1;
} else {
return 1;
}
}
}
return 0;
})).collect(Collectors.toList());
System.out.println(result);
Java8对List<Map<String,String>>自定义排序
最新推荐文章于 2024-04-12 09:10:25 发布