Why HashMap's get() returns null when it shouldn't?

I wrote a method to check if a string has only unique characters. I send it the obvious non-unique-characters string "11" and it returns true instead of false. It happens because in the get(c) in if (tab.get(c) == null) returns null even though the character '1' is already in the HashMap.

What can I do to get the expected behavior?

/* Check if a string contains only unique characters */
public static boolean isUniqueChars(String s) {

    HashMap<Boolean, Character> tab = new HashMap<Boolean, Character>();
    Character c;

    for (int i = 0; i < s.length(); ++i) {
        c = new Character(s.charAt(i));
        if (tab.get(c) == null)
            tab.put(Boolean.TRUE, c);
        else
            return false;
    }
    return true;
}

public static void main(String[] args) {

    String s = "11";
    System.out.println(isUniqueChars(s));  /* prints true! why?! */
}
Jon Skeet
people
quotationmark

You're fetching by character, but your map's key is Boolean. You want the key to be Character and the value to be Boolean:

HashMap<Character, Boolean> tab = new HashMap<Character, Boolean>();
Character c;

for (int i = 0; i < s.length(); ++i) {
    c = new Character(s.charAt(i));
    if (tab.get(c) == null)
        tab.put(c, Boolean.TRUE);
    else
        return false;
}
return true;

Having said that:

  • You don't need to create a new Character explicitly. Boxing will do that for you.
  • Using a HashSet<Character> to keep track of the characters you've seen so far would be simpler.

For example:

Set<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
    Character c = s.charAt(i);
    // add returns true if the element was added (i.e. it's new) and false
    // otherwise (we've seen this character before)
    if (!set.add(c)) {
        return false;
    }
}
return true;

people

See more on this question at Stackoverflow