3

I have a problem that I need help with. I have a HashSet that contains char[]. The problem is that I can't check if a value exists using the method contains(), it return false even if the value exists in the HashSet.

How can I resolve this problem ?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
mrjasmin
  • 1,230
  • 6
  • 21
  • 37
  • 1
    Take a look here: http://stackoverflow.com/questions/744735/java-array-hashcode-implementation – vanje Apr 21 '13 at 22:13

1 Answers1

8

You can't use char[] in a HashSet, since the implementation of hashCode() and equals for a char[] is identity-based, not content-based -- in other words, if two char[] arrays have the same contents, that doesn't mean their hash codes are the same. Use String instead.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413