Java List Group, Sort, etc.

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

Java List Group, Sort, etc.
[java] sort in list
Sort a List of Java objects
java bubble sort
Java memorandum (list)
Clone Java List.
java insertion sort
About List [Java]
Java Japanese (Kanji) Sort
About Java Array List
[Memo] Java Linked List
[Java] List type / Array type conversion
[Neta] Sleep Sort in Java
[Java, stream] Sort object list in Japanese by property name
Sort strings functionally with java
[Java] List OS-dependent standard libraries
List aggregation in Java (Collectors.groupingBy)
List data structure [Java / Scala]
Bubble sort using ArrayList (JAVA)
Sort List in descending order in Java and generate a new List non-destructively
[Java] Delete the elements of List
Java method list (memorial) (under construction)
[Java] Convert 1-to-N List to Map
List of members added in Java 9
Sort in List, for personal notes
[Java] How to use List [ArrayList]
Surprisingly deep Java list inversion-Stream processing
List of types added in Java 9
[Java] Conversion from array to List
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map