Efficient intersection of two List in Java?

后端 未结 8 553
南方客
南方客 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 13:14

    How about

    private List<String> intersect(List<String> A, List<String> B) {
        List<String> rtnList = new LinkedList<>();
        for(String dto : A) {
            if(B.contains(dto)) {
                rtnList.add(dto);
            }
        }
        return rtnList;
    }
    
    0 讨论(0)
  • 2020-11-28 13:17

    using retainAll if don't care occurrences, otherwise using N.intersection

    a = N.asList(12, 16, 16, 17, 19);
    b = N.asList(16, 19, 107);
    a.retainAll(b); // [16, 16, 19]
    N.println(a);
    
    a = N.asList(12, 16, 16, 17, 19);
    b = N.asList(16, 19, 107);
    a = N.intersect(a, b);
    N.println(a); // [16, 19]
    

    N is an utility class in AbacusUtil

    0 讨论(0)
提交回复
热议问题