0
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {

        //Use inputs from https://pastebin.com/DMQ6xqKe and assign to s * t, as stackoverflow limited character use
        String s = "";
        String t = "";

        System.out.println(isAnagram(s, t));
    }

    public static boolean isAnagram(String s, String t) {
        HashMap<Character, Integer> sMap = new HashMap<>();
        HashMap<Character, Integer> tMap = new HashMap<>();

        if(s.length() != t.length())
        {
            return false;
        }

        for(int i = 0; i < s.length(); i++)
        {
            if(sMap.containsKey(s.charAt(i)))
            {
                sMap.replace(s.charAt(i), sMap.get(s.charAt(i)) + 1);
            }
            else
            {
                sMap.put(s.charAt(i), 1);
            }
        }

        for(int i = 0; i < t.length(); i++)
        {
            if(tMap.containsKey(t.charAt(i)))
            {
                tMap.replace(t.charAt(i), tMap.get(t.charAt(i)) + 1);
            }
            else
            {
                tMap.put(t.charAt(i), 1);
            }
        }

        //FAILS BUT WHY!?!??!?
        for(int i = 0; i < s.length(); i++)
        {
            System.out.println("outside " + sMap.get(s.charAt(i)) + " compared to " + tMap.get(s.charAt(i)));
            if(sMap.get(s.charAt(i)) != tMap.get(s.charAt(i)))
            {
                System.out.println(sMap.get(s.charAt(i)) + " compared to " + tMap.get(s.charAt(i)));
                return false;
            }
        }

        //PASSES BUT PREVIOUS FAILS?!!?!??!
        // if(!sMap.equals(tMap))
        // {
        //     return false;
        // }

        return true;
    }
}

This code fails but the commented code using .equals works. I don't really understand. Logged the values, they matched but the check fails saying different values.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Help
  • 15
  • 1
  • 8
  • 2
    Try changing `if(sMap.get(s.charAt(i)) != tMap.get(s.charAt(i)))` to `if (!sMap.get(s.charAt(i)).equals(tMap.get(s.charAt(i))))` – Elliott Frisch Feb 18 '20 at 20:34

2 Answers2

2

The == operator compares two object references. The equals method of type Object does the same, but some classes override that method with new functionality. For example, "x" == "x" has the potential to fail, if they are different objects. "x".equals("x") would be true.

String x = "@";
String y = x;
x == y; // true, its the same reference.

Hope this helped?

Rigidity
  • 169
  • 10
  • "potential to fail" - just to make this clear, it will always fail if they are different objects. – Zabuzard Feb 18 '20 at 21:03
  • Note that your string example is not particulary good, as `"x" == "x"` must be `true` as well, due to string interning (caching, string pool), which Java must support according to the JLS. – Zabuzard Feb 18 '20 at 21:04
  • I figured; it was more the general idea I was trying to relay. – Rigidity Feb 19 '20 at 03:54
1

It is because of the implementation of equals method in HashMap. in this implementation tow maps are equal if each (key, value) pair in the first map equals to (key, value) pair in the second map. and in this implementation two key, value pair are equal if both key reference the same object and both value reference the same object. here because your map may contain same key, value but in different object they are not equal.

Tashkhisi
  • 2,070
  • 1
  • 7
  • 20