I have two collections of the same object, Collection
and Collection
. The required logic is as follow:
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.
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);
}
}