It checks .hashCode to find the bucket, and then uses .equals . List.equals returns true if all items are in the same order and .equals as .equals . ArrayList.hashCode will return the same value for two instances of ArrayList with the same elements, so it will find the right bucket, then use .equals and see that the list items are the same and in the same order.
For instance:
ArrayList<Integer> lis = new ArrayList<Integer>(); lis.add(2); lis.add(3); ArrayList<Integer> lis2 = new ArrayList<Integer>(); lis2.add(2); lis2.add(3); System.out.println(lis.equals(lis2));
It's also worth noting that you should never use a mutable object as a key in a HashMap . By changing the key, you can cause the bucket to be invalid. For example, if I do this:
map.put(lis, 7); lis.add(3); System.out.println(map.get(lis));
This is because adding another element changed the value of lis.hashCode() . When you put list, hashCode used to select the bucket. By adding a new item, you will change the bucket that it will use, but it will not change the record bucket that you have already added to the map. Addendum to the above:
map.put(lis, 7); lis.add(3); map.put(lis, 7); System.out.println(map.size());
He resolves another bucket a second time, so he sees it as the second element.
In this case, you should use Collections.unmodifiableList to βfreezeβ the list, add it, and then never touch it:
map.put(Collections.unmodifiableList(lis), 7);
Then, if you call get().add(3) :
map.get(7).add(3);
This will raise an UnsupportedOperationException .