我有两个ArrayList,其中一个包含ID号(1000或更多),在其他对象中,可能包含(一个或多个)或不包含来自另一个ArrayList的ID。
使用Java进行检查的最佳方法是什么?
建议使用Streams解决方案,可能有用:
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());
现在,intersection
列表仅包含事物2和3。
另外,如果需要,您可以并行处理。参见https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#parallelStream--
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句