HashMap realization principle and Source code analysis

Source: Internet
Author: User

1. Data Structure

In the data structure and algorithm, we introduce several commonly used data structures: arrays, linked lists, hash tables.

Array structure: its memory allocation is a contiguous memory space, it can occupy serious memory space, space complexity, time complexity is small, the advantage is easy to address, but the insertion, removal difficulties.

Linked list structure: its memory allocation is a series of discrete memory space, occupy less memory, space complexity is very small, time complexity is very large, its advantages are easy to insert and delete, addressing difficult.

Hash table: Hash table is a collection of arrays and the storage structure of the list, to meet the convenience of addressing, easy to operate, occupy less memory space, time complexity is small. The example is as follows:

A set of data {19,14,23,01,68,20,84,27,55,11,10,79} is known, followed by hash function h (key) = key MOD 13 and the list address method to handle the hash conflict, whose hash table is as follows:

The above is an array of length 13, each element stores a list of the head node, using hash function hash (key)%len to get the index position.

2, the definition of HashMap

The internal HashMap is implemented by a hash table:


The definition of the source code is as follows (great changes have been made to HashMap in JDK1.8):

Defines an array that stores node nodes.

<span style= "FONT-SIZE:14PX;" >/**     * The table, initialized on first use, and resized as     * necessary. When allocated, the length is always a power of.     * (We also tolerate length zero in some operations to allow     * bootstrapping mechanics that is currently not needed.)     */    transient node<k,v>[] table;</span>

Node nodes implement the Map.entry interface, which contains the Key,value,next,hash value.

<span style= "FONT-SIZE:14PX;"  >/** * Basic Hash bin node, used for most entries.     (See below for * TreeNode subclass, and in Linkedhashmap for its Entry subclass.)        */Static Class Node<k,v> implements map.entry<k,v> {final int hash;        Final K Key;        V value;        Node<k,v> Next;            Node (int hash, K key, V value, node<k,v> next) {This.hash = hash;            This.key = key;            This.value = value;        This.next = Next;        } Public final K GetKey () {return key;}        Public final V GetValue () {return value;}        Public final String toString () {return key + "=" + value;}        Public final int hashcode () {return Objects.hashcode (key) ^ Objects.hashcode (value);            Public final V SetValue (v newvalue) {v oldValue = value;            value = newvalue;        return oldValue; } Public Final Boolean equals (ObjectO) {if (o = = this) return true;                if (o instanceof map.entry) {map.entry<?,? > E = (map.entry<?,? >) o;                    if (Objects.equals (Key, E.getkey ()) && objects.equals (value, E.getvalue ()))            return true;        } return false; }}</span>

Where the Map.entry interface is as follows:



3. Key attributes of HashMap

The primary property of HashMap is the default initial size and load factor.

Default_load_factor = 0.75f; Default table Size

default_initial_capacity = 1 << 4; aka 16 resizing increment when larger than default size







HashMap realization principle and Source code analysis

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.