Skip to content

Instantly share code, notes, and snippets.

@priyankshah217
Last active November 21, 2021 15:36
Show Gist options
  • Save priyankshah217/f000a8498f36dbf8adca8c26d293aa1a to your computer and use it in GitHub Desktop.
Save priyankshah217/f000a8498f36dbf8adca8c26d293aa1a to your computer and use it in GitHub Desktop.
Java-8 Collectors Examples
package io.practice;
import java.util.*;
import java.util.function.Function;
import static java.util.stream.Collectors.*;
class Person {
String name;
String lastName;
Integer age;
List<String> listOfCars;
public Person(String name, String lastName, Integer age, List<String> listOfCars) {
this.name = name;
this.lastName = lastName;
this.age = age;
this.listOfCars = listOfCars;
}
public List<String> getListOfCars() {
return listOfCars;
}
public String getName() {
return name;
}
public String getLastName() {
return lastName;
}
public Integer getAge() {
return age;
}
}
public class CollectionExercises {
static List<Person> getListOfPerson() {
return List.of(new Person("Albert", "Smith", 32, List.of("Volkswagen", "Hyundai", "Suzuki")),
new Person("Bob", "Michael", 20, List.of("BMW", "Hyundai", "Volkswagen")),
new Person("Charlie", "Cohen", 10, List.of("Mercedes", "Toyota", "BMW")),
new Person("Charlie", "Mathew", 10, List.of("Suzuki", "Toyota", "Volkswagen")),
new Person("Emily", "James", 32, List.of("Suzuki", "Mercedes", "Hyundai")),
new Person("Flora", "James", 42, List.of("Hyundai", "BMW", "Toyota")),
new Person("Gary", "Luis", 56, List.of("Mercedes", "Hyundai", "Suzuki")),
new Person("Henry", "", 29, List.of("BMW", "Suzuki", "Volkswagen")),
new Person("Ian", "Smith", 61, List.of("Mercedes", "Hyundai", "Toyota")),
new Person("James", "Carlos", 39, List.of("Suzuki", "Hyundai", "Volkswagen")),
new Person("Kim", "Liu", 24, List.of("Toyota", "Mercedes", "Volkswagen"))
);
}
public static void main(String[] args) {
List<Person> personList = getListOfPerson();
// Person who has BMW cars, [Bob, Charlie, Flora, Henry]
List<String> getUserByCarNames = personList
.stream()
.collect(filtering(person -> person.listOfCars.contains("BMW"),
mapping(Person::getName,
toList())));
System.out.println(getUserByCarNames);
// Person Group by their age, {32=[Albert, Emily], 20=[Bob], 39=[James], 24=[Kim], 56=[Gary], 42=[Flora], 10=[Charlie, Charlie], 61=[Ian], 29=[Henry]}
Map<Integer, List<String>> groupUserByAges = personList
.stream()
.collect(groupingBy(Person::getAge, mapping(Person::getName,
toList())));
System.out.println(groupUserByAges);
// Find all unique cars, [Volkswagen, Toyota, Suzuki, Mercedes, Hyundai, BMW]
Set<String> getAllDistinctCars = personList
.stream()
.collect(flatMapping(person -> person.listOfCars.stream(),
toSet()));
System.out.println(getAllDistinctCars);
// Count persons' first names, {Gary=1, Emily=1, James=1, Bob=1, Flora=1, Ian=1, Charlie=2, Albert=1, Kim=1, Henry=1}
Map<String, Long> getUserCount = personList
.stream()
.collect(groupingBy(Person::getName,
counting()));
System.out.println(getUserCount);
// count how many no of cars have {Volkswagen=6, Toyota=5, Suzuki=6, Mercedes=5, BMW=4, Hyundai=7}
Map<String, Long> getTotalCars = personList
.stream()
.collect(flatMapping(person -> person.listOfCars.stream(),
toList()))
.stream()
.collect(groupingBy(Function.identity(),
counting()));
System.out.println(getTotalCars);
// Most used cars, Hyundai=7
Map.Entry<String, Long> mostUsedCar = Collections
.max(personList.stream()
.collect(flatMapping(person -> person.listOfCars.stream(),
toList()))
.stream()
.collect(groupingBy(Function.identity(),
counting()))
.entrySet(),
Map.Entry.comparingByValue());
System.out.println(mostUsedCar);
// Least used cars, BMW=4
Map.Entry<String, Long> leastUsedCar = Collections
.min(personList.stream()
.collect(flatMapping(person -> person.listOfCars.stream(),
toList()))
.stream()
.collect(groupingBy(Function.identity(),
counting()))
.entrySet(),
Map.Entry.comparingByValue());
System.out.println(leastUsedCar);
// Oldest person, Ian->61
Person oldestPerson = personList.stream().max(Comparator.comparingInt(Person::getAge)).orElse(null);
System.out.println(Objects.requireNonNull(oldestPerson).getName() + " -> " + oldestPerson.getAge());
// IntSummaryStatistics{count=11, sum=355, min=10, average=32.272727, max=61}
IntSummaryStatistics summaryOfAges = personList.stream()
.collect(summarizingInt(Person::getAge));
System.out.println(summaryOfAges);
// true => [Flora, Gary, Ian, James] false => [Albert, Bob, Charlie, Charlie, Emily, Henry, Kim]
Map<Boolean, List<String>> segregatePersonBasedOnAge = personList.stream()
.collect(partitioningBy(person -> person.getAge() > 32, mapping(Person::getName, toList())));
System.out.println(segregatePersonBasedOnAge.get(true));
}
}
@abhivaikar
Copy link

Interesting and really useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment