The Collectors class is used with Stream.collect() to package elements for a terminal operation. Something like this:
List<String> alphabet = Arrays.asList("a", "b", "c", "d"); List<String> otherAlphabet = alphabet.stream() .collect(toList());
The Collectors class has three important and useful methods: Collectors.toList, Collectors.toMap, and Collectors.toSet.
You have seen toList() above. Here is toMap().
Map<String, Integer> alphabetMap = alphabet.stream() .collect(toMap(letter -> letter, String::length))
Here is Collectors.toSet().
Set<String> result = alphabet.stream() .collect(toSet());
Here is Collectors.toCollection().
List<String> alphabetList = alphabet.stream() .collect(toCollection(LinkedList::new))
Here is Collectors.joining().
String joins = alphabet.stream() .collect(joining(" "));
"a b c d"
Here is Collectors.counting().
Long count = alphabet.stream() .collect(counting());
Result
4
Collectors.summarizingDouble() / Collectors.summarizingInt() / Collectors.summarizingLong(). This returns a SummaryStatistics class that contains statistical information about the elements.
public static void demoSummarizing() { DoubleSummaryStatistics alphabetStats = alphabet.stream() .collect(Collectors.summarizingDouble(letter -> letter.length())); System.out.println("average: "+alphabetStats.getAverage()); System.out.println("count: "+alphabetStats.getCount()); System.out.println("max: "+alphabetStats.getMax()); System.out.println("sum: "+alphabetStats.getSum()); System.out.println("min: "+alphabetStats.getMin()); }
Result
average: 1.0 count: 5 max: 1.0 sum: 5.0 min: 1.0
Collectors.groupingBy() is used for grouping objects by some property and storing results in a Map instance.
User[name=Aaron Holloway,age=16,height=6.626572070342046,gender=FEMALE] User[name=Nora Reyes,age=8,height=5.909744090999723,gender=FEMALE] User[name=Nia Allen,age=29,height=6.415385593055398,gender=MALE] User[name=Quinton Hoover,age=25,height=6.549790026231361,gender=MALE] User[name=Alannah Salinas,age=20,height=6.4459305179442605,gender=FEMALE] User[name=Jaylin Fleming,age=13,height=6.265460947673736,gender=FEMALE]
public static void demoGroupBy() { System.out.println("\ndemo group by\n"); Map<String, Set<User>> userMap = users.stream() .collect(Collectors.groupingBy(user -> user.getGender(), Collectors.toSet())); Set<User> males = userMap.get("MALE"); System.out.println("male size: "+males.size()+" . "+males); Set<User> females = userMap.get("FEMALE"); System.out.println("female size: "+females.size()+" . "+females); }
Result
demo group by male size: 2 . [User[name=Nia Allen,age=29,height=6.415385593055398,gender=MALE], User[name=Quinton Hoover,age=25,height=6.549790026231361,gender=MALE]] female size: 4 . [User[name=Aaron Holloway,age=16,height=6.626572070342046,gender=FEMALE], User[name=Jaylin Fleming,age=13,height=6.265460947673736,gender=FEMALE], User[name=Alannah Salinas,age=20,height=6.4459305179442605,gender=FEMALE], User[name=Nora Reyes,age=8,height=5.909744090999723,gender=FEMALE]]
Collectors.partioningBy() accepts a Predicate instance and collects Stream elements into a Map instance that stores Boolean values as keys and collections as values.
User[name=Johanna Dickson,age=22,height=6.4127723755001504,gender=FEMALE] User[name=Julian Jenkins,age=23,height=6.775657455958665,gender=FEMALE] User[name=Antwan Wheeler,age=26,height=6.637100653384031,gender=FEMALE] User[name=Samantha Parsons,age=14,height=5.622675954086426,gender=MALE] User[name=Nia Camacho,age=5,height=6.9896558044147294,gender=MALE] User[name=Giselle Morris,age=6,height=5.580378836408688,gender=MALE]
Result
demo partioning by adult size: 3 . [User[name=Johanna Dickson,age=22,height=6.4127723755001504,gender=FEMALE], User[name=Julian Jenkins,age=23,height=6.775657455958665,gender=FEMALE], User[name=Antwan Wheeler,age=26,height=6.637100653384031,gender=FEMALE]] youngens size: 3 . [User[name=Samantha Parsons,age=14,height=5.622675954086426,gender=MALE], User[name=Nia Camacho,age=5,height=6.9896558044147294,gender=MALE], User[name=Giselle Morris,age=6,height=5.580378836408688,gender=MALE]]