Java learning notes (II) -- Java HashMap

Source: Internet
Author: User
Tags rehash

[Previous] Get up and sleep in the morning. Recently, you should pay attention to the same job and rest status. HashMap has a good learning experience. [Definition] Hashmap: A hash, which stores key-value mappings. The null value and the null key are allowed. Java. lang. the Cloneable interface is an empty interface that specifies whether an object can be cloned. the object that implements this interface can call the clone () method for shortest object cloning. by default, java clones are shortest clones. Shortest clones only the objects to be considered, instead of the objects referenced by the shortest clones. serializable indicates that this class can be serialized. Java HashMap code: HashMap inherits the AbstracMap class, implements the Map interface, Cloneable interface, and Serializable interface. 1) HashMap: 1 public class HashMap <K, V> 2 extends AbstractMap <K, V> 3 implements Map <K, V>, Cloneable, Serializable 2) abstractMap 1 public abstract class implements actmap <K, V> implements Map <K, V> 3) Map 1 public interface Map <K, V> 4) cloneable 1 public interface Cloneable {// empty interface 2} 5) Serializable 1 public interface Serializable {// empty interface 2} [knowledge point] I. Data Structure of HashMap in java, the basic structure is two types. One is an array and the other is a simulated pointer (reference). All data structures can be It is constructed using these two basic structures, and hashMap is no exception. Hashmap is actually a "link hash" data structure, that is, a combination of arrays and linked lists. It can be seen that the bottom layer of HashMap is an array structure, and each item in the array is a linked list. When a new HashMap is created, an array is initialized. The source code is as follows: Copy code 1 static class Entry <K, V> implements Map. entry <K, V> {2 final K key; 3 V value; 4 Entry <K, V> next; 5 int hash; 6 • 7} copy the code, this is a usage of java generics. It is a key-value Pair and has a next Entry pointing to it, which constitutes a linked list. It is also a single linked list. 2. The HashMap constructor 1 public HashMap (int initialCapacity, float loadFactor) specifies the container size and loading factor. The constructor 1 public HashMap (int initialCapacity) specifies the container size, the default loading factor is 0.75 1 public HashMap (). The default constructor constructs an empty HashMap 1 public HashMap (Map <? Extends K ,? Extends V> m) to construct a new HashMap with the same ing relationship as the specified Map. 3. The API type function and function void clear () of HashMap remove all mappings from the ing. Object clone () returns a superficial copy of this HashMap instance: the key and value itself are not copied. Boolean containsKey (Object key) returns true if the ing includes a ing relationship for the specified key. Boolean containsValue (Object value) returns true if this ing maps one or more keys to a specified value. Set <Map. Entry <K, V> entrySet () returns the Set view of the ing contained in the ing. V get (Object key) returns the value mapped to the specified key. If the key does not contain any ing relationship, null is returned. Boolean isEmpty () returns true if the ing does not contain the key-value ing relationship. Set <K> keySet () returns the Set view of the keys contained in the ing. V put (K key, V value) associates the specified value with the specified key in this ing. Void putAll (Map <? Extends K ,? Extends V> m) copies all Mappings of the specified ing to this ing. These mappings replace all mappings currently mapped to all keys in the specified ing. V remove (Object key) removes the ing relationship of the specified key from this ing (if any ). Int size () returns the number of key-value ing relationships in the ing. Collection <V> values () returns the Collection view of the values contained in this ing. 4. The HashMap instance has two parameters that affect its performance: "initial capacity" and "loading factor ". 1. capacity is the length of the array in the hash table. The initial capacity is only the length of the hash table during creation. The default value is 16 1 static final int DEFAULT_INITIAL_CAPACITY = 1 <4; // aka 16 2. A load factor is a scale in which a hash table can reach full capacity before its capacity increases automatically. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, You need to rehash the hash table (that is, rebuild the internal data structure ), in this way, the hash table will have approximately two times the number of buckets. The default loading factor is 0.75, 1 static final float DEFAULT_LOAD_FACTOR = 0.75f; 5. Why does HashMap resize (rehash) set the initial capacity and loading factor: when there are more and more elements in a HashMap, the probability of Hash conflicts increases, and the length of the array is fixed by default, which is 16. To improve query efficiency, we need to resize the Hashmap array. After the hashmap is expanded, the most performance-consuming part will appear, the data in the original array must be recalculated and placed in the new array. This is resize. The default load factor is 0.75, which is a compromise between time and space costs. Although the loading factor is too high, it reduces the space overhead, but it also increases the query cost (this is reflected in most HashMap operations, including get and put operations ). When setting the initial capacity, you should take into account the number of entries and their loading factors required in the ing to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, the rehash operation is not performed. 6. The implementation of Hashmap is not synchronous, which means it is not thread-safe. Its key and value can all be null. In addition, the ing in HashMap is not sequential. HashMap is NOT thread-safe. It means that when multiple threads operate, the results may be unpredictable. We have learned in the operating system that the thread security status is absolutely safe, insecure status may cause unsafe phenomena, such as deadlocks, but may not. The ing of HashMao is not ordered. For example, the zipper method is as follows: when the zipper method is used to create a hash, the final hash is different from the order in the initial keyword code.

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.