Java map with integer keys: how are keys compared?

I just want to make sure my code is safe using Integer objects as keys. Here is a quick example:

 Integer int1 = new Integer(1337); Integer int2 = new Integer(1337); if (int1 == int2) { System.out.println("true"); } else { System.out.println("false"); } if (int1.equals(int2)) { System.out.println("true"); } else { System.out.println("false"); } Map<Integer, Object> map = new HashMap<Integer, Object>(); map.put(int1, null); map.put(int2, null); System.out.println(map.size()); 

As a result, the code outputs

 false true 1 

What I expected, the links are different, but they are equal to each other. Now I'm interested in the behavior of the Map.

  • Is it guaranteed that collections, such as Map or Set, will compare keys based on their contents, not a link?
  • Or does it depend on the actual implementation, for example HashMap ?
+6
source share
8 answers

The equals method is called, so it compares with the contents.

As for your two questions above:

Given the two objects o1 and o2 (to simplify, suppose that o1!=null and o2!=null ), the hasp map must ultimately determine whether they have the same value. (ultimately, since HaspMap also checks to see if o1 and o2 the same hash value, but that doesn't matter in the context of your question). He does this by calling the equals() method. While o1.equals(o2) is false, two objects are considered two different HashMap keys.

HashSet also calls equals() to determine if the item is in the set, see http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html#add%28E%29 .

TreeMap , on the other hand, must compare two objects and determine whether they are equal and which are larger. He does this by calling compareTo() . Therefore, the return value o1.compareTo(o2) (or if you created a tree map with the constructor http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html# TreeMap% 28java.util.Comparator% 29 , using a comparator).

Thus, it is guaranteed that in HashMap and HashSet the equals() method is used to specify objects separately, and in TreeMap , the compareTo() method is used.

+7
source

Q1: - Is it guaranteed that collections, such as Map or Set, will compare keys according to their contents, and not a link?

A1: - No. Collection, map and set are interfaces. The only thing they assure is the contract of possible methods.

Q2: - Depends on the actual implementation, for example HashMap?

A2: Yes. How the class relates to matching is the decision of the developer.

HashMap uses two things to place its objects.

First up is Object#hashCode() , which is used to calculate the index.

The second is Object#equals() , which is used, then a hash collision takes place.

+6
source

If you open implementation from java.util.AbstractMap , you will see that the equation of keys and values ​​is checked using the Object#equals method everywhere. What will actually be compared inside the map depends on the implementation of the key / value of the Object#equals method.

+3
source

Here, Integer is the final Waraper class, which overrides the equals() method, so it will only compare content.

So, if using integers is any wrapper class in Map

no problems.

Suppose that if you want to use Custome Class as a key, you need to override the equals() and hashcode() method to avoid duplication in Map

+1
source

Actually, it depends on the equals() K / key implementation that you specify for Map .

Try the same thing with Map<Object,String> and see what happens (hint: for Object , to be equal, they must actually be the same object)

Greetings

0
source

The first compares two different objects (links) -> false.

The second compares (equals) the values ​​of these objects -> true

Hashmap uses equals and hascode methods to define unique keys. so you have the same key inserted twice, leaving one remaining element. second. look at Map # put javadoc to see what happens.

0
source

In the case of hashmap, keys are compared using the equals () and hashcode () methods.

In the above example, both hashcode () and equals () are both overridden in the Integer class. When a HashMap compares two keys, it first takes the hashcode () of this object, and then calls the equals method for that object, and the key has the same hashcode value.

0
source

When placing an element inside a HashMap (and HashSet by extension), hashCode (converted by a simple linear function) is used to determine where the element should be placed inside the internal collection.

Then, in a certain place, he searches for an identical object (among all the elements stored there), using (o1 == o2 || o1.equals (o2)), the #equals function is called if the links are different, which is an increase in performance to a simple call #equals . If an identical element is found, its assigned value is replaced with a new one; if not, the new element is simply added to the internal collection.

0
source

All Articles