What happens when HashMap is full in Java?

Source: Internet
Author: User
Tags rehash

1, HashMap How to save datause an internal static class (Entry) array in HashMap table to store data, i.e. entry<k,v>[] table;
Static Class Entry<k,v> implements Map.entry<k,v> {        final K key;        V value;        Entry<k,v> Next;        int hash;               Entry (int h, K K, v V, entry<k,v> N) {            value = V;            Next = N;            key = k;            hash = h;        } }
The entry class holds the hash value of key, value, key, and a reference to the next entry (the Zipper method solves the hash conflict).

2. Put methodthe contents of the Put method
    Public V put (K key, V value) {        if (key = = null)            return Putfornullkey (value);//The value NULL for key is placed in the position of the array below table 0, the hash value is also 0. C3/>int hash = hash (key);        int i = indexfor (hash, table.length);        for (entry<k,v> e = table[i]; E! = null; e = e.next) {            Object K;            if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k))) {                V oldValue = e.value;                E.value = value;                E.recordaccess (this);                return oldValue;            }        }        modcount++;        AddEntry (hash, key, value, I);//See        return null below;    }
Put method processing is to find the key corresponding to the hash exists, if it exists to determine whether the key is the same, if key is the same as the new value to replace the old value.
If the hash for this key does not exist or the key is not the same, it is added to the position of table I in the array table
3. AddEntry MethodAddEntry method source code
    void AddEntry (int hash, K key, V value, int bucketindex) {        if (size >= threshold) && (null! = Table[bucket Index]) {            Resize (2 * table.length);            hash = (Null! = key)? Hash (key): 0;            Bucketindex = Indexfor (hash, table.length);        }        Createentry (hash, key, value, bucketindex);//add element in specified subscript    }
Threshold is the meaning of the threshold, this value is determined by two parameters: capacity (capacity, default 16, all subsequent values are 2 of the n-th square), loadfactor (load factor, default 0.75, simple to count is the space utilization), threshold= Capacity*loadfactor, such as capacity=16,loadfactor=0.75 by default, so threshold=12, when the number of elements in the HASHMAP is greater than or equal to 12 o'clock, it is possible to expand to twice times the original capacity, Use the Resize method to adjust.

4. Resize MethodResize method source code
    void Resize (int newcapacity) {        entry[] oldtable = table;        int oldcapacity = oldtable.length;        if (oldcapacity = = maximum_capacity) {            threshold = Integer.max_value;            return;        }        entry[] newtable = new entry[newcapacity];        Boolean oldalthashing = usealthashing;        Usealthashing |= sun.misc.VM.isBooted () &&                (newcapacity >= holder.alternative_hashing_threshold);        Boolean rehash = oldalthashing ^ usealthashing;        Transfer (newtable, rehash);//Transfer method        table = newtable;        threshold = (int) math.min (newcapacity * loadfactor, maximum_capacity + 1);    }

You can see that the maximum value of the threshold threshold is integer.max_value. When the current capacity capacity has reached the maximum value of 2^30, no expansion is done, and the threshold is set to the maximum value of int.if the maximum capacity is not reached, it is twice times larger, and the elements in the oldtable array are transferred to the new newtable array, and the threshold value is recalculated.

5. Transfer MethodTransfer method source code
void Transfer (entry[] newtable, Boolean rehash) {        int newcapacity = newtable.length;        for (entry<k,v> e:table) {            while (null! = e) {                entry<k,v> next = e.next;                if (rehash) {                    E.hash = NULL = = E.key? 0:hash (E.key);                }                int i = indexfor (E.hash, newcapacity);                E.next = Newtable[i];                Newtable[i] = e;                e = Next;}}}    



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

What happens when HashMap is full in Java?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.