-4

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;
}
twenk11k
  • 557
  • 5
  • 17
  • 1
    What are you trying to do? –  Apr 08 '19 at 12:25
  • for example i have [1, 2] in both hashmaps. I check before if statement. It's same but if statement always return false. – twenk11k Apr 08 '19 at 12:30
  • 1
    Possible duplicate of [Comparing two Collections in Java](https://stackoverflow.com/questions/4085353/comparing-two-collections-in-java) – Amongalen Apr 08 '19 at 12:37
  • `hashMap.values()` is not equal to `hashMap2.values()` because the first is [2,1,3] and the second is [1,2,3]. The order in the values collection is determined by the order of keys and in the case of `HashMap` it's virtually unpredictable. – Klitos Kyriacou Apr 08 '19 at 12:45
  • @KlitosKyriacou I was added wrong values. s = "egg" and t = "add" have same values but it's not correct to check via .values().equals() because collections cannot be equal as Ali Ben Zarrouk pointed as answer. Thanks for helping. – twenk11k Apr 08 '19 at 12:50

2 Answers2

2

You're comparing two collections, they will never be equal because you're doing a reference compare on two different collections. Apache commons-collections has CollectionUtils#isEqualCollection, use that. Of course if you're creating you own class as value of the map, implement equals and hashCode in it.

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
  • This is not the correct answer. The OP's code uses `equals()`, not `==`. There is no reference comparison in the code. – DodgyCodeException Apr 08 '19 at 12:47
  • 3
    @DodgyCodeException The `Collection` returned by `HashMap.values()` does not override `equals`, so it uses the default implementation, which compares the references. – Eran Apr 08 '19 at 12:48
2

Since hashMap.values() is return Collection<T> we cannot compare it with equals(). Thanks to Ali Ben Zarrouk for help. Here is the implementation of how to check if values of two hashmaps are equal or not without using any third party library.

   if(areHashMapValuesEqual(hashMap.values(),hashMap2.values())){
        return true;
    } else {
        return false;
    }

  static <T> boolean areHashMapValuesEqual(Collection<T> lhs, Collection<T> rhs) {
    boolean equals = false;
    if(lhs!=null && rhs!=null) {
        equals = lhs.size( ) == rhs.size( ) && lhs.containsAll(rhs)  && rhs.containsAll(lhs);
    } else if (lhs==null && rhs==null) {
        equals = true;
    }
    return equals;
}
twenk11k
  • 557
  • 5
  • 17
  • This is good but I would name the method something like `equalsIgnoreOrder`. Otherwise it could be confusing as the same name `equals` in the `List` interface is dependent on order. – DodgyCodeException Apr 08 '19 at 13:57