Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet usage with int arrays

Tags:

java

hashset

As I have an ArrayList of int arrays which contains duplicates, I'd like to use HashSet. Unfortunately, I can't manage to use HashSet as I wish:

System.out.print("\nTESTs\n");
    ArrayList<int[]> list = new ArrayList<int[]>();
    list.add(new int[]{1,2,3});
    list.add(new int[]{5,1,1});
    list.add(new int[]{1,2,3});//duplicate
    list.add(new int[]{5,1,3});

    Set<int[]> set = new HashSet<int[]>(list);
    System.out.println("Size of the set = "+set.size());

    ArrayList<int[]> arrayList = new ArrayList<int[]>(set);
    System.out.println("Size of the arrayList = "+arrayList.size());

    for (int[] array:arrayList){
        System.out.println(Arrays.toString(array));
    }

It results in:

Size of the set = 4
Size of the arrayList = 4
[1, 2, 3]
[1, 2, 3] // duplicate still here
[5, 1, 1]
[5, 1, 3]

Could anybody tell me where I'm wrong ?

Thanks in advance Dominique (java newbie)

like image 718
Dominique Avatar asked Feb 05 '15 12:02

Dominique


People also ask

Does HashSet work for array?

... is, correct: HashSet is not sensitive to the content of an array.

Is array faster than HashSet?

HashSet is designed to have expected constant time add , contains and remove operations, meaning that the time won't change much regardless of how many elements are in the set. Arrays have linear operations for all of these, but lower overhead. This means that arrays will generally be better for small sets.

How do you declare an integer array?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.

Can you assign an array of int to an array of integer?

To initialize an integer array, you can assign the array variable with new integer array of specific size as shown below. arrayName = new int[size];


2 Answers

Arrays don't override hashCode and equals implemented in Object class, and therefore, two arrays a1 and a2 will be considered as identical to each other by HashSet only if a1==a2, which is false in your case.

If you use ArrayLists instead of arrays, your problem will be solved, since for ArrayLists equality is determined by the equality of the members of the lists (and the order in which they appear).

like image 176
Eran Avatar answered Sep 18 '22 23:09

Eran


That's because HashSet uses .equals() to see if a new object is duplicated (and .hashCode() to determine the "bucket").

When you work with arrays, please be aware that new int[]{1,2,3} is NOT "equal to" new int[]{1,2,3}.

The proper way to "deep compare" arrays is via Arrays.equals(a, b) method.

To solve your problem situation effectively you should create a wrapper class which contains your int[] array and then implement .hashCode() and equals() properly.

like image 27
vikingsteve Avatar answered Sep 22 '22 23:09

vikingsteve