Storing arrays in Set and avoiding duplicates Storing arrays in Set and avoiding duplicates arrays arrays

Storing arrays in Set and avoiding duplicates


You can't. arrays use the default identity-based Object.hashCode() implementation and there's no way you can override that. Don't use Arrays as keys in a HashMap / HashSet!

Use a Set of Lists instead.


The "better way" is to use collections. Use a List instead of a String[]:

Set<List<String>> boog = //...boog.add(Arrays.asList("a", "b", "c"));boog.add(Arrays.asList("a", "b", "c"));boog.add(Arrays.asList("a", "b", "d"));System.out.println(boog.size()); // 2

Edit

If you absolutely needed to use arrays as keys, you could build a transparent wrapper around each key and put that in the map. Some libraries help you with that. For example, here's how you could do a Set<String[]> using Trove:

Set<String[]> boog = new TCustomHashSet<String[]>(new ArrayHashingStrategy());boog.add(new String[]{"a", "b", "c"});boog.add(new String[]{"a", "b", "c"});boog.add(new String[]{"a", "b", "d"});System.out.println(boog.size()); // 2//...public class ArrayHashingStrategy extends HashingStrategy<Object[]> {   public int computeHashCode(Object[] array) {      return Arrays.hashCode(array);   }   public boolean equals(Object[] arr1, Object[] arr2) {      return Arrays.equals(arr1, arr2);   }}        


hashCode() of arrays uses the default implementation, which doesn't take into account the elements, and you can't change that.

You can use a List instead, with a hashCode() calculated based on the hashcodes of its elements. ArrayList (as most implementations) uses such function.


Alternatively (but less preferable, unless you are forced somehow to use arrays), you can use a 'special' HashSet where instead of invoking key.hashCode() invoke Arrays.hashCode(array). To implement that extend HashMap and then use Collections.newSetFromMap(map)