I\'m trying to group Java objects by their field, i.e Person.java
public class Person {
String name;
String surname;
....
}
Using java-8 with the Collectors class and streams, you can do this:
Map> mapByName =
allPeople.stream().collect(Collectors.groupingBy(Person::getName));
List allDavids = mapByName.getOrDefault("David", Collections.emptyList());
Here I used getOrDefault so that you get an empty immutable list instead of a null reference if there is no "David" in the original list, but you can use get if you prefer to have a null value.
Hope it helps! :)