I took a peek at the contents of Java's HashMap

Read the Java HashMap Source Code (https://github.com/AdoptOpenJDK/openjdk-jdk8u/blob/master/jdk/src/share/classes/java/util/HashMap.java) and read the hash Confirmed that the key comparison is done with equals () .

    /**
     * Implements Map.get and related methods.
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;                            //Internal array
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {                                //Of the internal array(hashCode/Array length remainder)The second stored element(First Node of LinkedList)
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {                                                                 //Search the Linked List one by one
                    if (e.hash == hash &&                                            //If the hashCode is different, go to the next Node(optimization purpose)      
                        ((k = e.key) == key || (key != null && key.equals(k))))      //Here equals()Compare with!!!!!!
                        return e;
                } while ((e = e.next) != null);     
            }
        }
        return null;
    } 

It was confirmed that the object under search and the object with the key of each Node of the LinkedList under search are compared for consent using their respective equals ().

reference

Recommended Posts

I took a peek at the contents of Java's HashMap
I took a look at the resources of Azure Container Instance
I want to var_dump the contents of the intent
I managed to get a blank when I brought the contents of Beans to the textarea
I wrote a sequence diagram of the j.u.c.Flow sample
I learned about the existence of a gemspec file
I want to be aware of the contents of variables!
I tried to create a log reproduction script at the time of apt install
Let's take a look at the screen of Quant Analyzer!
I tried JAX-RS and made a note of the procedure
I want to get a list of the contents of a zip file and its uncompressed size
[Ruby] How to retrieve the contents of a double hash
Why put a line break at the end of the file
I tried to take a look at the flow of Android development environment construction with Android Studio
I think I understand the reuse of cells, but I don't understand at all.
A solution to the problem of blank spaces at the beginning of textarea
I made a gem to post the text of org-mode to qiita
I made a tool to output the difference of CSV file
How to check for the contents of a java fixed-length string
[Rails] Check the contents of the object
Replace the contents of the Jar file
I read the source of ArrayList I read
I read the source of Integer
A memorandum of the FizzBuzz problem
I read the source of Long
I read the source of Short
I read the source of Byte
I read the source of String
[Ruby] Display the contents of variables
How to change the value of a variable at a breakpoint in intelliJ
After all I wanted to preview the contents of mysql with Docker ...
Take a peek at how Processing 3 works
Check the contents of the Java certificate store
Check the contents of params with pry
Memo: [Java] Check the contents of the directory
Folding and unfolding the contents of the Recyclerview
I investigated the internal processing of Retrofit
[day: 5] I summarized the basics of Java
[Ruby] Cut off the contents of twitter-ads
Format the contents of LocalDate with DateTimeFormatter
The contents of the data saved by CarrierWave.
Find the difference from a multiple of 10
What I tried when I wanted to get all the fields of a bean
I want to see the contents of Request without saying four or five
Let's take a look at the functions of Keycloak's management console (administrator edition)
I want to recursively get the superclass and interface of a certain class
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...