From Sets public boolean equals(Object o)
Compares the specified object with this set for equality. Returns true
if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.
This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it returns false. If so, it returns containsAll((Collection) o).
Note: But in your given example your selectedSet
doesn't have same element.
I think you want to check if all element in selectedSet
set is part of masterMap.keySet()
For that there is no stranded API you have to iterate over you set selectedSet
and check it in masterMap.keySet()
if it exists. like below code:
boolean compareMapAndSet = checkSubSet(masterMap.keySet(), selectedSet);
private static boolean checkSubSet(Set<Integer> keySet, Set<Integer> selectedSet) {
for (Integer integer : selectedSet) {
if (!keySet.contains(integer)) {
return false;
}
}
return true;
}
See the output result