简体   繁体   中英

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.

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, eg 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM