mrjasmin
mrjasmin

Reputation: 1270

Java - HashSet with char[] elements

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 ?

Upvotes: 4

Views: 5830

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198471

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.

Upvotes: 9

Related Questions