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
  • 24,752
  • 31
  • 93
  • 118
mrjasmin
  • 1,190
  • 5
  • 16
  • 34
  • 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
  • 182,351
  • 25
  • 326
  • 397