Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package com.technologyconversations.java8exercises.streams;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static*;
public class Grouping {
public static Map<String, List<Person>> groupByNationality7(List<Person> people) {
Map<String, List<Person>> map = new HashMap<>();
for (Person person : people) {
if (!map.containsKey(person.getNationality())) {
map.put(person.getNationality(), new ArrayList<>());
return map;
public static Map<String, List<Person>> groupByNationality(List<Person> people) {
return // Convert collection to Stream
.collect(groupingBy(Person::getNationality)); // Group people by nationality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment