[Java] Map/hashmap-Source Code Learning notes

Source: Internet
Author: User

Map

1. The object used to correlate key and value, where key and key cannot be duplicated.

2. is an interface used to replace Dictionary abstract classes in earlier versions of Java.

3. Three different views are provided to observe the internal data, the set view of key, the Collection view of value, and the set view of the associated object Key-value.

4. Some implementations will guarantee the order of elements, such as TreeMap. Some are not guaranteed, such as HashMap

5. If key is a mutable object, you need to handle it carefully

6. The key value points to the map itself is not allowed, but the value values point to the map itself is run.

7. Starting with Java 8, many Default methods have been introduced to the Map interface. The goal is to support the new functional programming introduced by Java 8, without breaking the existing code that inherits the Map interface. No discussion is conducted here.

HashMap

1. A hash table-based MAP interface implementation.

2. Allow null as key or value

3. Order of elements not guaranteed

4. Provides operation in constant time, including get,put operation

5. There are two built-in parameters that affect the performance of the HASHMAP: initial capacity and Loadfactor

6. Iterative process, using fail-fast mechanism. See ArrayList Fail-fast for details.

7. A hash value for a box (bin). When multiple hash values of different elements are equal, they belong to the same box.

HASHMAP uses a tree structure to handle large amounts of hash-value collisions. The tree refers to the red and black trees.

8. tablesizefor (int) Returns the value of a power that is greater than the minimum 2 of the parameter. For example Tablesizefor (3) returns 4, Tablesizefor (5) returns 8.

9. Remove (object) deletes the record with object key and returns the associated value value. Returns null if the element that needs to be deleted cannot be found. Note that there are two cases of returning NULL, one is that the element cannot be found, and the other is the Object association value itself is null.

RemoveNode (int hash, object key, Object value, Boolean Matchvalue, Boolean movable) is a method that implements the delete algorithm, which is called by remove (object). The algorithm ideas are as follows:

A. Find the corresponding box according to the hash

B. Locate the object in the box that you want to delete. Depending on the storage structure of the elements in the box (list or tree), different search methods are used.

C. After finding the object to be deleted, the call takes a different node deletion policy depending on the storage structure.

The HASHMAP data structure relationship is simple, as follows.

KeySet (), values () and EntrySet () return the view of key, value, entry, respectively. Because only one view of the internal data, the view and the HASHMAP use the same piece of data. Therefore, modifications to elements on the view are also reflected in the HashMap, and vice versa.

12. The underlying iterator is hashiterator. The other three integrators: Keyiterator, Valueiterator, Entryiterator, are all inherited from the Hashiterator iterator.

Afternodeaccess, Afternodeinsertion, Afternoderemoval is a post-processing application for Linkedhashmap, which is not processed in HashMap.

JDK version: JDK1.8.0_31.JDK

[Java] Map/hashmap-Source Code Learning notes

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.