Antony Vimal Raj
Antony Vimal Raj

Reputation: 384

Java 8 Hashmap Internals

I have gone through the implementation of Java 8 Hashmap and arrived with below doubts. Please help me to clarify them:

  1. I read in an article which says that, nodes with the same hashcode will be added in same bucket as a linked list. It says that the new node to this linked list will be added in head insteadof tail to avoid tail traversal. But when I see the source code, new node is getting added in tail. Is that correct?
  2. I didn't completely understand this variable MIN_TREEIFY_CAPACITY. Is it like after this much count, entire map will be converted to tree(from array to tree)?

Upvotes: 4

Views: 744

Answers (2)

codebusta
codebusta

Reputation: 2053

Peter Lawrey's response regarding MIN_TREEIFY_CAPACITY is WRONG.

This constant is defaulted to 64 and used in the java.util.HashMap#treeifyBin method which is called if a bucket's size is larger than 8 (TREEIFY_THRESHOLD).

Inside the java.util.HashMap#treeifyBin method, if the hash table size is less than 64 then the entire table is RESIZED - doubled, otherwise, the bucket under question is TREEIFIED - bucket's DS - linked list is converted to binary tree.

The whole point is to maintain O(1) insertion or lookup - if the hash table size is small (64) we can easily resize it by doubling it, thus the range for the buckets will be doubled and there will be less collisions for hash keys and each bucket will have less items.

If the table size is greater than 64, than it might be expensive to double the hash table size, it is better just to convert the current bucket's linked list into binary tree bucket which is faster to search from (linked list search is O(n) whereas binary tree search is O(log(n)).

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

But when I see the source code, new node is getting added in tail. Is that correct?

It is added to the head, in older versions. However, many changes were made in Java 8 which does.

class A {
    static class SameHash {
        final int n;

        SameHash(int n) {
            this.n = n;
        }

        @Override
        public int hashCode() {
            return 1;
        }

        @Override
        public String toString() {
            return "SameHash{" +
                    "n=" + n +
                    '}';
        }
    }

    public static void main(String[] args) {
        HashSet<SameHash> set = new HashSet<>();
        for (int i = 1; i <= 4; i++)
            set.add(new SameHash(i));
        System.out.println(set);
    }
}

prints

[SameHash{n=1}, SameHash{n=2}, SameHash{n=3}, SameHash{n=4}]

NOTE: Keys can have different hashCodes but can end up in the same bucket.

I didn't completely understand this variable MIN_TREEIFY_CAPACITY. Is it like after this much count, entire map will be converted to tree(from array to tree)?

After this count, the bucket is converted to a tree provided the key is Comparable

Upvotes: 6

Related Questions