HashMap explanation of the forward Java data structure

Source: Internet
Author: User
Tags rehash

Overview

In this chapter, we study the HashMap.
We first have a general understanding of HashMap, and then learn its source code, and finally through the example to learn to use HashMap. The content includes:
Part 1th introduction of HashMap
Part 2nd HASHMAP data structure
Part 3rd HashMap Source parsing (based on jdk1.6.0_45)
Section 3.1 of the HashMap "zipper method" related content
Part 3.2 HashMap Constructors
Part 3.3 Main external interface of HashMap
Part 3.4 HashMap Implementation of the Cloneable interface
Part 3.5 HashMap Implementation of the Serializable interface
Part 4th HASHMAP Traversal mode
Part 5th HashMap Example

Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3310835.html

Part 1th introduction of HashMap

HashMap Introduction

HashMap is a hash table that stores content that is a key-value pair (key-value) mapping.
HashMap inherits from Abstractmap, and realizes the map, cloneable, java.io.Serializable interface.
The implementation of the HASHMAP is not synchronous, which means it is not thread-safe. Both its key and value can be null. In addition, the mappings in HashMap are not ordered.

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor . Capacity is the number of buckets in the hash table, and the initial capacity is just the capacity at the time of creation of the Hashtable. A load factor is a scale in which a hash table can reach a full amount before its capacity increases automatically. When the number of entries in the hash table exceeds the product of the load factor with the current capacity, the Hashtable is rehash (that is, rebuilding the internal data structure) so that the Hashtable will have about twice times the number of buckets.
Typically, the default load factor is 0.75, which is a tradeoff in time and space costs. The high load factor, while reducing the space overhead, also increases the query cost (which is reflected in the operations of most HASHMAP classes, including get and put operations). When setting the initial capacity, you should take into account the number of entries required in the mapping and their loading factors to minimize the number of rehash operations. The rehash operation does not occur if the initial capacity is greater than the maximum number of entries divided by the load factor.

The HashMap constructor

HashMap a total of 4 constructors , as follows:

The default constructor. HASHMAP ()//Specifies the "capacity size" of the constructor HashMap (int capacity)//Specifies the "Capacity size" and "load factor" constructors HashMap (int capacity, float loadfactor)//contains " Sub Map "constructor HashMap (map<? extends K,? extends V> Map)

HashMap's API

void                 Clear () object               Clone () Boolean              ContainsKey (Object key) Boolean              Containsvalue (object value) Set <entry<k, v>>     entryset () v                    get (Object key) Boolean              isEmpty () set<k>               KeySet () v                    put (k key, V value) void                 putall (map<? extends K,? extends v> Map) V                    Remove (Object key) int                  size ( ) collection<v>        VALUES ()

part 2nd HASHMAP data Structure

The inheritance relationship of HashMap

Java.lang.Object   ?     Java.util.abstractmap<k, v>         ?     Java.util.hashmap<k, V>public class hashmap<k,v>    extends abstractmap<k,v>    implements Map <k,v>, cloneable, Serializable {}

HashMap and map relationships such as :

Can be seen:
HashMap inherits from the Abstractmap class and implements the map interface. Map is the "Key-value key-value pair" interface, and Abstractmap implements a universal function interface for "key-value pairs".
(HashMap) is a hash table implemented by the "zipper Method". It includes several important member variables: table, size, threshold, Loadfactor, Modcount.
Table is a entry[] array type, and entry is actually a one-way list. The hash table's "Key-value Key-value pairs" are stored in the entry array.
Size is HashMap, which is the number of key-value pairs saved by HashMap.
The threshold is the HASHMAP threshold that is used to determine whether the hashmap capacity needs to be adjusted. The value of threshold = "capacity * load factor", when the amount of data stored in HashMap reaches threshold, it is necessary to double the capacity of HashMap.
Loadfactor is the loading factor.
Modcount is used to implement the fail-fast mechanism.

Part 3rd HashMap Source parsing (based on jdk1.6.0_45)

In order to better understand the principle of hashmap, the following HashMap source code to make an analysis.
In reading the source code, it is recommended to refer to the following instructions to establish a general understanding of HashMap , so that easier to understand HashMap.

View Code

Description :

Before we elaborate on HashMap's code, we need to understand thatHashMap is a hash table that solves the conflict by "zipper ".
It is also important to note that there are two parameters that affect HashMap performance: initial capacity (initialcapacity) and load factor (Loadfactor). Capacity is the number of buckets in the hash table, and the initial capacity is just the capacity at the time of creation of the Hashtable. A load factor is a scale in which a hash table can reach a full amount before its capacity increases automatically. When the number of entries in the hash table exceeds the product of the load factor with the current capacity, the Hashtable is rehash (that is, rebuilding the internal data structure) so that the Hashtable will have about twice times the number of buckets.


Section 3.1 of the HashMap "zipper method" related content

3.1.1 HashMap data Storage Array

Transient entry[] table;

The key-value in HashMap are stored in the entry array .

3.1.2 Data structure of the node entry

View Code

From this, we can see that Entry is actually a one-way linked list. This is also why we say that HashMap is solved by the zipper method of the conflict.
Entry implements the Map.entry interface, which implements Getkey (), GetValue (), SetValue (V value), Equals (Object O), and hashcode () functions. These are basic functions that read/modify key and value values.

Part 3.2 HashMap Constructors

HashMap Total includes 4 constructors

View Code

Part 3.3 Main external interface of HashMap

3.3.1 Clear ()

The clear () function is to empty the HashMap. It is done by setting all the elements to null.

View Code

3.3.2 ContainsKey ()

The role of ContainsKey () is to determine if HashMap contains a key.

public boolean ContainsKey (Object key) {    return getentry (key)! = NULL;}

ContainsKey () First obtains the entry of the key by Getentry (key)and then determines whether the entry is null.
The source code of Getentry () is as follows:

View Code

The function of Getentry () is to return the key value pair of key , which has been explained in the source code.
It should be emphasized here that theHashMap "key is null" is placed in the table position 0 , that is, table[0], "key is not NULL" in the rest of the table!


3.3.3 containsvalue ()

The role of Containsvalue () is to determine whether HashMap contains an element that is value-valued .

View Code

From this we can see that containsnullvalue () is processed in two steps: First, if "value is null", then Containsnullvalue () is called. Second, if "value is not NULL", look for a node in HashMap that has value values.

The role of Containsnullvalue () determines whether the HashMap contains a value of NULL element .

View Code

3.3.4 EntrySet (), values (), KeySet ( )

They are similar in principle to the 3, which is illustrated in the example of EntrySet ().
The function of EntrySet () is to return "the collection of all entry in HashMap", which is a collection. The implementation code is as follows:

View Code

HashMap is a hash table implemented by the Zipper method. Performance in HashMap includes many entry, and each entry is essentially a one-way linked list. So how do hashmap traverse key-value key-value pairs?


Let's see how HashMap is traversed by EntrySet ().
EntrySet () is actually implemented through Newentryiterator (). Let's take a look at its code:

View Code

NextEntry () is actually called when we go through the EntrySet () iterator Next () method to traverse the HashMap. The implementation of NextEntry () first traverses the entry (according to the sequence number of entry in the table, from small to large traversal), and then to each entry (that is, each one-way linked list), and then iterate through each.


3.3.5 get ()

The function of Get () is to get the value corresponding to key, and its implementation code is as follows:

View Code

3.3.6 put ()

The function of put () is to provide an external interface so that the HashMap object can add "Key-value" to HashMap by put () .

View Code

To add a key value pair to the HashMap, the corresponding key already exists in HashMap, then the key value pair is found, then the new value replaces the old value and exits!
To add a key value pair to the HashMap, the corresponding key is not in HashMap, it is added to the linked list for that hash value, and addentry () is called.
Here's a look at the code for AddEntry ():

View Code

The role of AddEntry () is to add entry. Insert "Key-value" into the specified position, Bucketindex is the position index.

When it comes to AddEntry (), you have to say another function, Createentry (). The code for Createentry () is as follows:

View Code

Their role is to add key and value to the HashMap. And, comparing the Code of AddEntry () and Createentry (), we find that AddEntry () has two more sentences:

if (size++ >= threshold)    Resize (2 * table.length);

So what is the difference between them?
Reading the code, we can see that their usage scenarios are different.
AddEntry () is generally used in cases where the new entry may result in "HashMap actual capacity" exceeding the threshold value .
For example, we create a new HashMap and then constantly add elements to the HashMap through put (), and put () is added entry by AddEntry ().
In this case, we do not know when "the actual capacity of the HashMap" will exceed the "threshold value";
Therefore, you need to call AddEntry ()
Createentry () is generally used in cases where the new entry does not cause the "HashMap actual capacity" to exceed the threshold value .
For example, we call the HashMap "with map" constructor, which plots all the elements of the map to be added to the HashMap;
But before we add it, we've calculated "HashMap capacity and threshold." That is, you can be sure that even if all the elements in the map are added to HashMap, they will not exceed the hashmap threshold.
At this point, call Createentry ().

3.3.7 Putall ()

The function of Putall () is to add all the elements of "M" to the HashMap , and its code is as follows:

View Code

3.3.8 Remove ()

Remove () removes the "key to key" element

View Code

Part 3.4 HashMap Implementation of the Cloneable interface

HashMap implements the Cloneable interface, that is, the Clone () method is implemented.
The Clone () method acts simply by cloning a HashMap object and returning it.

View Code

Part 3.5 HashMap Implementation of the Serializable interface

HashMap realizes Java.io.Serializable, and realizes serial read and write function respectively.
The serial write function is writeobject (), which is written to the HashMap "total capacity, actual capacity, all entry" into the output stream.
and the serial read function is readobject (), its role is to HashMap "Total capacity, actual capacity, all the entry" read out in turn

View Code

Part 4th hashmap traversal mode

4.1 Traversing HashMap Key-value pairs

The first step: Get the set set of HashMap's "key-value pairs" based on EntrySet ().
Step Two: iterate through the set of "first steps" through the iterator iterator.

Assume that map is a HashMap object//The key in map is a string type, and value is an integer type integer integ = null;iterator iter = Map.entryset (). Iterator (); while (Iter.hasnext ()) {    Map.entry Entry = (map.entry) iter.next ();    Get key    key = (String) entry.getkey ();        Gets the value    Integ = (Integer) entry.getvalue ();}

4.2 Keys for traversing HashMap

The first step: get the set set of "Keys" for HashMap based on keyset ().
Step Two: iterate through the set of "first steps" through the iterator iterator.

Assume that map is a HashMap object//The key in map is a string type, value is an Integer type string key = Null;integer Integ = null;iterator iter = Map.keyset () . iterator (); while (Iter.hasnext ()) {        //get key    key = (String) iter.next ();        Based on key, get value    Integ = (Integer) map.get (key);}

4.3 Traversing the value of the HashMap

The first step: gets the collection of HashMap "values" based on value ().
Step Two: iterate through the set of "first steps" through the iterator iterator.

Assume that map is a HashMap object//The key in map is a string type, and value is an integer type integer value = null; Collection C = map.values (); Iterator iter= C.iterator (); while (Iter.hasnext ()) {    value = (Integer) iter.next ();}

The Traverse test program is as follows :

View Code

Part 5th HashMap Example

Here's an example to learn how to use HashMap

View Code

Run results (one time):

Map:{two=7, one=9, three=6}next:two-7next:one-9next:three-6size:3contains key Two:truecontains key Five:fa Lsecontains value 0:falsemap:{two=7, One=9}map is empty

HashMap details of the forward Java data structure

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.