HashMap bottom implementation principle, and the comparison __java Foundation with Hashtable

Source: Internet
Author: User
Tags int size rehash

Boolean	Containsvalue (Object value)
Returns True if this map maps one or more keys to the specified value.

First of all, we need to know HashMap bottom implementation is array (entry type) plus linked list of data structure---zipper method to implement hash table

Entry implements the Map.entry interface, which implements Getkey (), GetValue (), SetValue (V value), Equals (Object O), hashcode () These functions


1. Through the hash () function, calculate the key corresponding hash value, find the storage location in the array, in the conflict (different keywords, corresponding to the same hash value, in) fewer cases, his query efficiency is very high, if there is a conflict in the array elements in the list of the table header insert the value, So in search of HashMap when there are two key functions, one is the hash () function to find the key in the array position, one is the Equals () function, found in the list key, return value, usually return a keyword when the judgment method: (Key==null?) K==null:key.equlas (k)), the visible keyword can be null

An instance of 2.Hashtable has two parameters affecting its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is the size of the hashtable when it was created. Note that the status of the hash table is open: When a hash conflict occurs, a single bucket stores multiple entries that must be searched sequentially. The load factor is a measure of how full a hash table can be when its capacity is automatically increased. Both the initial capacity and the load factor are just hints for the implementation. Specific details about when and whether to call the rehash method depend on the implementation. Typically, the default load factor is 0.75 (when the number of data elements reaches 75% of the total size of the array, the group is dynamically expanded), which is to find a tradeoff between time and space costs. The load factor is too high although it reduces the space overhead, it also increases the time to find an entry (which is reflected in most Hashtable operations, including get and put operations).

3.public class Hashmap<k,v> extends abstractmap<k,v> implements Map<k,v>, HashMap inherits from the Abstractmap parent class and is not a thread-synchronized


Hashtable and HashMap adopt the Hash/rehash algorithm are probably the same, so there is no significant difference in performance.

About the difference between HashMap and Hashtable:

1. Because of historical reasons, Hashtable is based on the dictionary class, HashMap is the implementation of the map interface after 1.2 (from its naming of the nonstandard can be seen in the years. Normal should be Hashtable, if modified, a large number of programs need to be modified.

2. The most important difference is that Hashtable is synchronized (each method is preceded by a synchronized keyword), and hashmap is not thread-synchronized and can take advantage of the Collections class Synchronizedmap () Method creates a secure map object and returns it as a encapsulated object that allows you to synchronize access to potential hashmap.

     Map m = collections.synchronizedmap (new HashMap (...));

In 3.HashMap, Key,value is allowed to be empty, that is, only one record in HashMap can be an empty key, but any number of entries can be empty value. Or if the search key is found, but it is an empty value, then get (object key) returns NULL. If necessary, use the Containkey () method to distinguish between these two situations. The Hashtable key,value is not allowed to be empty.



Here is a list of some HashMap common methods:

Boolean ContainsKey (Object key) Returns True if this map contains a mapping for the specified key.

Boolean Containsvalue (Object value) Returns True if this map maps one or more keys to the specified value. <span style= "Background-color:rgb (255, 204, 102);"

>Set<Map.Entry<K,V>> entryset () </span> Returns a Set view of the mappings contained in this MAP. <span style= "Background-color:rgb (255, 204, 102);" > v get (Object key) </span> Returns the "value to which" specified key is mapped, or null if this map conta

Ins no mapping for the key.
Boolean IsEmpty () Returns True if this map contains no key-value mappings. <span style= "Background-color:rgb (255, 204, 102);"

> set<k> keyset () </span> Returns a Set view of the keys contained in this map. <span style= "Background-color:rgb (255, 204, 102);"

>v put (K key, V value) </span> Associates The specified value with the specified key in this map. V Remove (Object key) removesThe mapping for the specified key from this map if present. Boolean remove (Object key, object value) removes the entry for the specified key only if it is currently mapped to the SPE

Cified value.

V Replace (K key, V value) replaces the "entry for" specified key only if it's currently mapped to some value.

int size () Returns The number of key-value mappings in this map.
 Collection<v> values () Returns a Collection view of the values contained in this map.




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.