Dark Horse Programmer--java Foundation--Collection (query table map Collection)

Source: Internet
Author: User
Tags rehash

--java Training, Android training, iOS training,. NET training look forward to sharing with you! ——

Collection Operations-Query table 1. Query Table 1.1. Map interface

Java provides a set of data structures that can store data in the form of a key-value pair (key-value), which becomes a map. We can think of map as a multi-row, two-column table, where the first column holds the key and the second column holds the value.
Each row is equivalent to a set of key-value pairs that represent a set of data.
The map has a requirement for the elements to be stored, that is, key cannot be duplicated, so-called cannot repeat refers to the map cannot contain two equals is true key.
Map is not strictly required for key,value types, as long as the reference type is available. But in order to ensure that there is no data clutter in use, we typically use generics to constrain the type of key and value.

1.1.1. Put method

Now that we know that the map is actually storing the data in two pieces of information, key and value. So let's take a look at how to deposit data into a map.
Map provides a way to:

V put(K k,V v)
The function of this method is to store the Key-value pair in the map, since the duplicate key is not allowed in the map, so if the key that was deposited is already present in the map, the value operation is replaced, and the return value is the replaced element. If the secondary key does not exist, then the return value is null.

1.1.2. Get Method method

We learned how to store data in maps, so let's take a look at how to get the data. The way to get data in a map is to get the corresponding value for the given key.
Map provides a way to:

V get(Object key)
The function of this method is to find the corresponding value in the map according to the given key and return it, if the current map does not contain the given key, then the return value is null.

1.1.3. ContainsKey method

The ContainsKey method in map is used to detect whether a given key is contained in the current map. The method is defined as follows:

boolean containsKey(Object key)
If the current map contains a given key (check whether the inclusion is based on the equals comparison result of key). ) returns True.

1.2. HashMap1.2.1. Hash Table principle

HashMap is a common subclass implementation of map. In fact, the hash algorithm is used to achieve.
HashMap internally maintains a hash array (that is, an array of stored elements), which we call a hash bucket, and when we deposit a set of key-value pairs into the HashMap, HashMap first gets the return value of the Hashcode () method of the Key object. The value is then used to make a hash algorithm, which is a number that is the set of key-value pairs to be stored in the hash array in the subscript position.
When you know the subscript position, HashMap also checks to see if the current location of the hash array contains the element. (note here that each element in the hash array is not a direct store of a key-value pair, but rather a linked list in which each node in the list is actually holding the set of key-value pairs.) ) checks if the element is included, depending on whether the key currently being stored in the current hash array corresponds to the link in the list, if it does not contain the set of key-value pairs in the linked list, or replace value.
Then, when the element is fetched, HashMap also hashes the algorithm based on the hashcode value of the key, finds its position in the hash array, and then iterates through the linked list of the position, and returns the value corresponding to the key.
See here may have a question, the list should only be stored in one element, then HashMap is how to put Key-value into a linked list of a node? In fact, HashMap encapsulates each set of key-value pairs as an instance of entry and then stores the instance in a linked list.

1.2.2. Hashcode method

HashMap access is the return value of the Hashcode method that relies on key, and the Hashcode method is actually defined in object. It is defined as follows:
int hashCode()
The Hashcode () method of overriding a class has the following considerations:
If a class overrides the Equals method, then the Hashcode () method should be overridden.
If the Equals method of two objects is compared to true, then they should have the same hashcode value.
For the same object, multiple calls to the Hashcode () method should always return the same value if the content has not changed.
For two objects, equals is false, and does not require that its hashcode value be different, but should be as different as possible, thus improving the performance of the hash table.

1.2.3. Loading factor and HashMap optimization

There are nouns in the hash table that need to be understood:
Capacity: capacity, the number of buckets (barrels) in the hash table, that is, the hash array size.
Initial capacity: Initial capacity, the number of initial buckets when the hash table was created, and the default build capacity is 16. You can also use a specific capacity.
Size: The amount of data that is stored in the current hash table.
Load factor: The load factor, the default value of 0.75 (that is, 75%), expands and re-hashes (rehash) if the value of size/capacity is greater than the load factor when adding data to the hash table.
Then, when the load factor is smaller than the child, the hash lookup performance increases, and the hash bucket space capacity is wasted. 0.75 is the result of a relative balance between performance and space. When creating a hash table, specify a reasonable capacity, which reduces rehash to improve performance.

1.3. Orderly Map1.3.1. Linkedhashmap to achieve an orderly map

The hash table and linked list implementations of the Map interface have a predictable sequence of iterations. The difference between this implementation and HASHMAP is that Linkedhashmap maintains a two-way loop linked list. This list defines the order of the iterations, which is usually the order in which the elements will be held.
It is important to note that if you re-deposit the key in the map, the position of the key will not change, just replace the value.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--java Foundation--Collection (query table map Collection)

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.