I am trying to check whether values of two hashmaps are equal to each other or not with using hashMap.values().equals(). But even if two HashMap have same values it's not considered as equal.
String s = "egg";
String t = "add";
int count = 0;
HashMap<String,Integer> hashMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
String val = String.valueOf(s.charAt(i));
if (!hashMap.containsKey(val)) {
count++;
hashMap.put(val, count);
} else {
hashMap.put(val, hashMap.get(val));
}
}
HashMap<String,Integer> hashMap2 = new HashMap<>();
int count2 = 0;
for (int j = 0; j < t.length(); j++) {
String val = String.valueOf(t.charAt(j));
if (!hashMap2.containsKey(val)) {
count2++;
hashMap2.put(val, count2);
} else{
hashMap2.put(val, hashMap2.get(val));
}
}
if (hashMap.values().equals(hashMap2.values())) {
return true;
} else {
return false;
}