"Go" Java Collection series 10 HashMap detailed introduction (source parsing) and usage examples

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 ()//  Specify the constructor for "Capacity size" HashMap (int  capacity)//  Constructors that specify the capacity size and load factor HashMap (intfloat  loadfactor)//  include "sub map" Constructor for extendsextends v> map)

HashMap's API

voidClear () Object clone ()BooleanContainsKey (Object key)BooleanContainsvalue (Object value) Set<entry<k, v>>EntrySet () V get (Object key)BooleanisEmpty () Set<K>KeySet ()v put (K key, v value)voidPutall (map<?extendsK?extendsV>map) V Remove (Object key) intsize () 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>publicclass hashmap<k,v>    extends Abstractmap<k,v>    implements Map<k,v>, cloneable, Serializable {}

"Go" Java Collection series 10 HashMap detailed introduction (source parsing) and usage examples

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.