Today I was doing a question and in that they have used a code similar to this.
I am amazed to see this. I thought every HashSet
stores the hash of an object and the answer would be 2. However, the answer to this 1.
Could anyone explain what actually happens internally when I store HashSet
of ArrayList
of objects and why the answer is 1 instead of 2?
import java.io.*;
import java.util.*;
class Code {
public static void main (String[] args) {
HashSet<ArrayList<Integer>> set=new HashSet<>();
ArrayList<Integer> list1=new ArrayList<>();
ArrayList<Integer> list2=new ArrayList<>();
list1.add(1);
list1.add(2);
list2.add(1);
list2.add(2);
set.add(list1);
set.add(list2);
System.out.println(set.size()); // 1
}
}