How to groupBy object properties and map to another object using Java 8 Streams?

How to groupBy object properties and map to another object using Java 8 Streams?

In Java 8, you can use the Collectors.groupingBy() collector to group objects by a specified property, and then use Collectors.mapping() to transform the grouped elements into another object. Here's how you can do it:

Let's say you have a list of Person objects with properties name, age, and city, and you want to group them by city and then map the grouped elements into a list of PersonInfo objects containing city and a list of Person objects in that city.

import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Person> people = List.of( new Person("Alice", 30, "New York"), new Person("Bob", 25, "London"), new Person("Charlie", 35, "New York"), new Person("Dave", 40, "London") ); Map<String, List<Person>> peopleByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity)); // Map the grouped elements into PersonInfo objects List<PersonInfo> personInfos = peopleByCity.entrySet().stream() .map(entry -> new PersonInfo(entry.getKey(), entry.getValue())) .collect(Collectors.toList()); personInfos.forEach(System.out::println); } } class Person { private String name; private int age; private String city; public Person(String name, int age, String city) { this.name = name; this.age = age; this.city = city; } public String getCity() { return city; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } class PersonInfo { private String city; private List<Person> people; public PersonInfo(String city, List<Person> people) { this.city = city; this.people = people; } @Override public String toString() { return "PersonInfo{" + "city='" + city + '\'' + ", people=" + people + '}'; } } 

In this example:

  • We first group the Person objects by city using Collectors.groupingBy(Person::getCity), which creates a Map<String, List<Person>> where the keys are the cities and the values are lists of Person objects in each city.
  • Then, we use map() to transform the Map<String, List<Person>> into a list of PersonInfo objects containing the city and the list of Person objects in that city.
  • Finally, we collect the results into a list using Collectors.toList().

Examples

  1. "Java 8 stream group by and map example" Description: This query seeks examples of using Java 8 Streams to group objects by their properties and map them to another object. Below is a code snippet demonstrating this:

    import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StreamGroupByAndMapExample { public static void main(String[] args) { List<Person> persons = List.of( new Person("John", 30), new Person("Jane", 25), new Person("Doe", 30) ); Map<Integer, List<String>> groupedByNameLength = persons.stream() .collect(Collectors.groupingBy( person -> person.getName().length(), Collectors.mapping(Person::getName, Collectors.toList()) )); System.out.println(groupedByNameLength); } } class Person { private String name; private int age; // Constructor, getters, setters } 
  2. "Java 8 stream group by and map to object" Description: This query looks for ways to group objects by their properties using Java 8 Streams and map them to another object. Here's a code snippet demonstrating this:

    import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StreamGroupByAndMapToObject { public static void main(String[] args) { List<Person> persons = List.of( new Person("John", 30), new Person("Jane", 25), new Person("Doe", 30) ); Map<Integer, List<PersonDto>> groupedByAge = persons.stream() .collect(Collectors.groupingBy( Person::getAge, Collectors.mapping(person -> new PersonDto(person.getName()), Collectors.toList()) )); System.out.println(groupedByAge); } } class Person { private String name; private int age; // Constructor, getters } class PersonDto { private String name; public PersonDto(String name) { this.name = name; } // Getters } 
  3. "Java 8 stream group by and map with multiple properties" Description: This query aims to group objects by multiple properties using Java 8 Streams and map them to another object. Below is a code snippet demonstrating this:

    import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class StreamGroupByAndMapWithMultipleProperties { public static void main(String[] args) { List<Person> persons = List.of( new Person("John", 30), new Person("Jane", 25), new Person("Doe", 30) ); Map<Integer, Map<String, List<PersonDto>>> groupedByAgeAndNameLength = persons.stream() .collect(Collectors.groupingBy( Person::getAge, Collectors.groupingBy( person -> person.getName().length(), Collectors.mapping(person -> new PersonDto(person.getName()), Collectors.toList()) ) )); System.out.println(groupedByAgeAndNameLength); } } // Person and PersonDto classes as in the previous examples 

More Tags

jwplayer pow talkback vpn codeigniter-2 collision message homescreen find-occurrences lexer

More Programming Questions

More Housing Building Calculators

More Chemical thermodynamics Calculators

More Various Measurements Units Calculators

More Auto Calculators