Efficient intersection of two List in Java?

后端 未结 8 587
南方客
南方客 2020-11-28 12:23

Question is simple:

I have two List

List columnsOld = DBUtils.GetColumns(db, TableName);
List columnsNew = DBUtils.GetCol         


        
8条回答
  •  爱一瞬间的悲伤
    2020-11-28 12:57

    Using Google's Guava library:

    Sets.intersection(Sets.newHashSet(setA), Sets.newHashSet(setB))
    

    Note: This is much more efficient than naively doing the intersection with two lists: it's O(n+m), versus O(n×m) for the list version. With two million-item lists it's the difference between millions of operations and trillions of operations.

提交回复
热议问题