Search code examples
javacollectionsstream

Compare two Collections populated with different kinds of objects


I have a performance issue trying to compare two big Collections and I'm looking for some help to find a better way to do that.

The classes:

public class TypeOne {
   private int id;
}

and

public class TypeTwo {
   private int id;
}

The code:

Collection<TypeOne> oneColl = someMethodToPopulateThat();
Collection<TypeTwo> twoColl = anotherMethodToPopulateThat();

// Iterating both collections to match the elements by id
for(TypeOne one : oneColl) {
   for(TypeTwo two : twoColl) {
      if (one.getId().equals(two.getId())) 
         System.out.println(one.getId());
   }
}

I already tried to use some functions of Stream API but I didn't get success. Does anyone have any idea to solve this issue? Please.

Thanks in advance.


Solution

  • tl;dr

    ones.stream().forEach(
        one -> System.out.println(
            twos.stream().filter( two -> two.getId() == one.getId() ).findAny().toString()
        )
    )
    

    Details

    I assume sorting as a NavigableSet will improve performance of our searching, though I’ve not verified this attempt at optimization works.

    NavigableSet < TypeOne > ones = new TreeSet <>( Comparator.comparingInt( TypeOne :: getId ) );
    ones.addAll( collectionOfOnes ) ;
    
    NavigableSet < TypeTwo > twos = new TreeSet <>( Comparator.comparingInt( TypeTwo :: getId ) );
    twos.addAll( collectionOfTwos ) ;
    

    Loop one navigable set while searching for a match in the other.

    for( TypeOne one : ones )
    {
        Optional<TypeTwo> optionalTwo = twos.stream().filter( two -> two.getId() == one.getId() ).findAny() ;
        // handle your Optional which may or may not contain an object. 
    }