I have two ArrayLists, one contains id numbers (1000 or more) and in the other objects which might contain (one or more) or not ids from the other ArrayList.
What is the best way to check this using Java?
I have two ArrayLists, one contains id numbers (1000 or more) and in the other objects which might contain (one or more) or not ids from the other ArrayList.
What is the best way to check this using Java?
A suggested solution using Streams, maybe useful:
class Thing{
public int id;
public Thing( int id ){ this.id = id;}
public String toString() { return ""+id; }
}
List<Integer> ids = new ArrayList<Integer>();
List<Thing> things = new ArrayList<Thing>();
ids.add(1);
ids.add(2);
ids.add(3);
ids.add(4);
things.add( new Thing( 2 ) );
things.add( new Thing( 3 ) );
List<Thing> intersection = ids.stream().flatMap( id -> things.stream().filter(
thing -> id == thing.id
).map(
thing -> thing
)).collect(Collectors.toList());
Now the intersection
list contains the Things 2 and 3 only.
Plus, you can parallelize processing if required. See https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#parallelStream--