Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of two collections of different objects types java 8

I have two lists of objects:

List<SampleClassOne> listOne;
List<SampleClassTwo> listTwo;

SampleClassOne:

public class SampleClassOne{
  private String myFirstProperty;
  //ommiting getters-setters
}

SampleClassTwo:

public class SampleClassTwo{
  private String myOtherProperty;
  //ommiting getters-setters
}

RootSampleClass:

public class RootSampleClass{
  private SampleClassOne classOne;
  private SampleClassTwo classTwo;
  //ommiting getters-setters
}

Now I would like to merge two lists into new list of type RootSampleClass based on condition:

if(classOneObject.getMyFirstProperty().equals(classTwoObject.getMyOtherProperty()){
 //create new RootSampleClass based on classOneObject and classTwoObject and add it to another collection
}

Pseudo code:

foreach(one: collectionOne){
 foreach(two: collectionTwo){
    if(one.getMyFirstProperty().equals(two.getMyOtherProperty()){
    collectionThree.add(new RootSampleClass(one, two));
    }
 }
}

I am interested in java 8. I would like to have the best performance here that's why I am asking for existing solution without writing custom foreach.

like image 207
Gazeciarz Avatar asked Jun 01 '16 13:06

Gazeciarz


1 Answers

A direct equivalent to the nested loops is

List<RootSampleClass> result = listOne.stream()
    .flatMap(one -> listTwo.stream()
        .filter(two -> one.getMyFirstProperty().equals(two.getMyOtherProperty()))
        .map(two -> new RootSampleClass(one, two)))
    .collect(Collectors.toList());

with an emphasis on direct equivalent, which includes the bad performance of doing n×m operations.

A better solution is to convert one of the lists into a data structure supporting an efficient lookup, e.g. a hash map. This consideration is independent of the question which API you use. Since you asked for the Stream API, you can do it like this:

Map<String,List<SampleClassOne>> tmp=listOne.stream()
    .collect(Collectors.groupingBy(SampleClassOne::getMyFirstProperty));
List<RootSampleClass> result = listTwo.stream()
    .flatMap(two -> tmp.getOrDefault(two.getMyOtherProperty(), Collections.emptyList())
        .stream().map(one -> new RootSampleClass(one, two)))
    .collect(Collectors.toList());

Note that both solutions will create all possible pairings in case, a property value occurs multiple times within either or both lists. If the property values are unique within each list, like IDs, you can use the following solution:

Map<String, SampleClassOne> tmp=listOne.stream()
    .collect(Collectors.toMap(SampleClassOne::getMyFirstProperty, Function.identity()));
List<RootSampleClass> result = listTwo.stream()
    .flatMap(two -> Optional.ofNullable(tmp.get(two.getMyOtherProperty()))
            .map(one -> Stream.of(new RootSampleClass(one, two))).orElse(null))
    .collect(Collectors.toList());

If you don’t mind potentially performing double lookups, you could replace the last solution with the following more readable code:

Map<String, SampleClassOne> tmp=listOne.stream()
    .collect(Collectors.toMap(SampleClassOne::getMyFirstProperty, Function.identity()));
List<RootSampleClass> result = listTwo.stream()
    .filter(two -> tmp.containsKey(two.getMyOtherProperty()))
    .map(two -> new RootSampleClass(tmp.get(two.getMyOtherProperty()), two))
    .collect(Collectors.toList());
like image 172
Holger Avatar answered Sep 22 '22 22:09

Holger