Group List items into Several Lists using Stream in Java 8 is easy.
We have a list of Persons and we want to group them into Female, Male, Unknown list using Stream and Collectors in Java 8.
PersonBean
Below is a simple class that represents a person. It has a name and gender fields. The toString()
method has been overridden to display its fields’ contents in human-readable form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package streamexample.malefemale; /** * Simple JavaBean that represents a person. * * @author turreta.com * */ public class PersonBean { private String name; // female, male, or unknown private String gender; public PersonBean(String name, String gender) { super(); this.name = name; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "PersonBean [name=" + name + ", gender=" + gender + "]"; } } |
Main Class
Below is the main class that uses Stream
, and Collectors
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package streamexample.malefemale; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class MaleAndFemaleDemo { public static void main(String[] args) { final String FEMALE = "female"; final String MALE = "male"; final String UNKNOWN = "unknown"; List<PersonBean> listOfPersons = new ArrayList<>(); listOfPersons.add(new PersonBean("Aiza", FEMALE)); listOfPersons.add(new PersonBean("Rowena", FEMALE)); listOfPersons.add(new PersonBean("Mirsa", FEMALE)); listOfPersons.add(new PersonBean("Allan", MALE)); listOfPersons.add(new PersonBean("Pedro", MALE)); listOfPersons.add(new PersonBean("Rody", MALE)); listOfPersons.add(new PersonBean("Bot1", UNKNOWN)); listOfPersons.add(new PersonBean("QA", UNKNOWN)); listOfPersons.add(new PersonBean("TestUser", UNKNOWN)); Map<String, List<PersonBean>> myMap = splitByGender(listOfPersons.stream()); myMap.forEach((k, v) -> System.out.println("Gender : " + k + ", Listing : " + v)); } public static Map<String, List<PersonBean>> splitByGender(Stream<PersonBean> stream) { return stream.collect(Collectors.groupingBy(person -> person.getGender())); } } |
Output
myMap
will have 3 lists for each gender – male, female, and unknown.
1 2 3 | Gender : female, Listing : [PersonBean [name=Aiza, gender=female], PersonBean [name=Rowena, gender=female], PersonBean [name=Mirsa, gender=female]] Gender : male, Listing : [PersonBean [name=Allan, gender=male], PersonBean [name=Pedro, gender=male], PersonBean [name=Rody, gender=male]] Gender : unknown, Listing : [PersonBean [name=Bot1, gender=unknown], PersonBean [name=QA, gender=unknown], PersonBean [name=TestUser, gender=unknown]] |
Useful links
http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html