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

前端 未结 8 2170
生来不讨喜
生来不讨喜 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:36

    For comaparing a list or set we can use Arrays.equals(object[], object[]). It will check for the values only. To get the Object[] we can use Collection.toArray() method.

    0 讨论(0)
  • 2020-12-03 01:37
    public static boolean doCollectionsContainSameElements(
            Collection<Integer> c1, Collection<Integer> c2){
    
        if (c1 == null || c2 == null) {
            return false;
        }
        else if (c1.size() != c2.size()) {
            return false;
        } else {    
            return c1.containsAll(c2) && c2.containsAll(c1);
        }       
    }
    
    0 讨论(0)
提交回复
热议问题