How Best to Compare Two Collections in Java and Act on Them?

前端 未结 8 2178
生来不讨喜
生来不讨喜 2020-12-03 00:41

I have two collections of the same object, Collection oldSet and Collection newSet. The required logic is as follow:

8条回答
  •  隐瞒了意图╮
    2020-12-03 01:37

    public static boolean doCollectionsContainSameElements(
            Collection c1, Collection c2){
    
        if (c1 == null || c2 == null) {
            return false;
        }
        else if (c1.size() != c2.size()) {
            return false;
        } else {    
            return c1.containsAll(c2) && c2.containsAll(c1);
        }       
    }
    

提交回复
热议问题