Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 stream - merge two collections keeping it unique by specific field [duplicate]

I have two lists of dogs - dogsList1, dogsList2
I want to create a single list of all dogs with unique name field.
(meaning, if I encounter a second dog with the same name I do not add it to the result list)

This is the best I could do in java, but it collects unique names, not Dogs:
(Dog contains other fields than name)

// collect all dogs names from first list
List<String> dogNames1 = dogsList1.stream()
     .map(x -> x.getName()).collect(Collectors.toList()); 

dogList2.stream()
     .filter(x->!dogNames1.contains(x.getName()))
     .forEach( x->
            dogsList1.add(x);
     );

Can it be improved ? Any other better solution or optimized approach?

like image 769
ran632 Avatar asked Jan 08 '18 14:01

ran632


1 Answers

You can use merge multiple stream and drop duplicates.

For the first dog of a given name, you can do

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class A {

    public static void main(String[] args) {
        List<Dog> dogList1 = Arrays.asList(new Dog("a", 1), new Dog("b", 2), new Dog("f", 3));
        List<Dog> dogList2 = Arrays.asList(new Dog("b", 4), new Dog("c", 5), new Dog("f", 6));
        List<Dog> dogList3 = Arrays.asList(new Dog("b", 7), new Dog("d", 8), new Dog("e", 9));
        List<Dog> dogs = new ArrayList<>(
                Stream.of(dogList1, dogList2, dogList3)
                        .flatMap(List::stream)
                        .collect(Collectors.toMap(Dog::getName,
                                d -> d,
                                (Dog x, Dog y) -> x == null ? y : x))
                        .values());
        dogs.forEach(System.out::println);
    }
}

class Dog {
    String name;
    int id;

    public Dog(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

prints

Dog{name='a', id=1}
Dog{name='b', id=2}
Dog{name='c', id=5}
Dog{name='d', id=8}
Dog{name='e', id=9}
Dog{name='f', id=3}

In each case, you can see the first instance of a name is retained.

For unique names

Set<String> names = Stream.of(dogList1, dogList2, dogList3)
                          .flatMap(List::stream)
                          .map(Dog::getName)
                          .collect(Collectors.toSet());
like image 158
Peter Lawrey Avatar answered Oct 12 '22 22:10

Peter Lawrey