public static void main(String[] args) {
ArrayList<LinkedHashMap> maps = new ArrayList<>();
LinkedHashMap<String, Object> map1 = new LinkedHashMap<>();
map1.put("statu","F");
map1.put("time","2022-07-10 19:25:00");
LinkedHashMap<String, Object> map2= new LinkedHashMap<>();
map2.put("statu","F");
map2.put("time","2022-07-10 19:25:00");
LinkedHashMap<String, Object> map3= new LinkedHashMap<>();
map3.put("statu","T");
map3.put("time","2022-07-10 19:20:00");
maps.add(map1);
maps.add(map2);
maps.add(map3);
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("data",maps);
List<LinkedHashMap> data = (List<LinkedHashMap>) stringObjectHashMap.get("data");
ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.convertValue((List) stringObjectHashMap.get("data"), new TypeReference<List<people>>() {});
List<people> list = JSONObject.parseArray(JSONObject.toJSONString(stringObjectHashMap.get("data"))).toJavaList(people.class);
//当两条数据分组条件相同和排序字段值相同时 会丢失相同的数据
Map<String, List<people>> collect = list.stream()
.collect(Collectors.groupingBy(people::getStatu, toSortedList(
Comparator.comparing(people::getTime).reversed())));
Map<String, List<people>> collect1 = list.stream().sorted(Comparator.comparing(people::getTime))
.collect(Collectors.groupingBy(people::getStatu));
System.out.println("----------collect-----------"+collect;
System.out.println("----------collect1-----------"+collect1);
}
public static <T> Collector<T, ?, List<T>> toSortedList(Comparator<? super T> c) {
return Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(c)), ArrayList::new);
}
}
````
java8 分组排序数据丢失
最新推荐文章于 2023-06-07 20:31:24 发布