ryber ryber - 1 year ago 69
Java Question

Group and Reduce list of objects

I have a list of objects with many duplicated and some fields that need to be merged. I want to reduce this down to a list of unique objects using only Java 8 Streams (I know how to do this via old-skool means but this is an experiment.)

This is what I have right now. I don't really like this because the map-building seems extraneous and the values() collection is a view of the backing map, and you need to wrap it in a new

to get a more specific collection. Is there a better approach, perhaps using the more general reduction operations?

public void reduce() {
Collection<Foo> foos = Stream.of("foo", "bar", "baz")
.collect(Collectors.toMap(f -> f.name, f -> f, (l, r) -> {
return l;

assertEquals(3, foos.size());
foos.forEach(f -> assertEquals(10, f.ids.size()));

private Stream<Foo> getfoos(String n) {
return IntStream.range(0,10).mapToObj(i -> new Foo(n, i));

public static class Foo {
private String name;
private List<Integer> ids = new ArrayList<>();

public Foo(String n, int i) {
name = n;

Answer Source

If you break the grouping and reducing steps up, you can get something cleaner:

Stream<Foo> input = Stream.of("foo", "bar", "baz").flatMap(this::getfoos);

Map<String, Optional<Foo>> collect = input.collect(Collectors.groupingBy(f -> f.name, Collectors.reducing(Foo::merge)));

Collection<Optional<Foo>> collected = collect.values();

This assumes a few convenience methods in your Foo class:

public Foo(String n, List<Integer> ids) {
    this.name = n;

public static Foo merge(Foo src, Foo dest) {
    List<Integer> merged = new ArrayList<>();
    return new Foo(src.name, merged);