Sorting process by dividing List into groups
//group
Map<String, List<Object>> map = list.stream().collect(//
Collectors.groupingBy(o -> o.getKey1()));
//Multiple keys
Map<String, List<Object>> map = list.stream().collect(//
Collectors.groupingBy(o -> o.getKey1() + o.getKey2()));
//Supports multiple key nulls
Map<String, List<Object>> map = list.stream().collect(//
Collectors.groupingBy(t -> t.getKey1() + (t.getKey2() == null ? "" : t.getKey2())));
//sort
Comparator<Object> comparator = //
Comparator.comparing(Object::getSort1, Comparator.nullsFirst(Comparator.naturalOrder()))//
.thenComparing(Object::getSort2, Comparator.nullsFirst(Comparator.naturalOrder()))//
.thenComparing(Object::getSort3, Comparator.nullsFirst(Comparator.naturalOrder()));
// limit
map.entrySet().forEach(e -> e.getValue().stream().sorted(comparator).limit(100).forEach(l -> System.out.println(e.getKey() + ":" + l.getValue())));
Recommended Posts