Bhavya Arora
Bhavya Arora

Reputation: 800

HashMap returning false always for containsValue(),

I am implementing a HashMap and it always returns false even if the character is repeating. I tried the solution that was given in other Stack Overflow problem, but didn't help, same is the case with Hashtable with function contains().

 HashMap<Character, Boolean> ht=new HashMap<Character, Boolean>();
    for(int i=0; i<s.length(); i++){
        if(!ht.containsValue(new Character(s.charAt(i))))
            ht.put(new Character(s.charAt(i)),true);
        else
            return false;
    }
    return true;

Upvotes: 0

Views: 397

Answers (1)

sag
sag

Reputation: 5451

As mentioned in comments, use containsKey()

 HashMap<Character, Boolean> ht=new HashMap<Character, Boolean>();
    for(int i=0; i<s.length(); i++){
        if(!ht.containsKey(new Character(s.charAt(i))))
            ht.put(new Character(s.charAt(i)),true);
        else
            return false;
    }
    return true;

Upvotes: 2

Related Questions