Use of Map

Source: Internet
Author: User

1.Map Interface

The set of map interface definitions, also known as query tables, is used to store so-called "key-value" mapping pairs. A key can be considered an index of value, and an object that is a key cannot be duplicated in the collection.

According to the internal structure, the map interface has a variety of implementation classes, in which the common inner classes are the HashMap implemented by the hash table and the TREEMAP implemented internally for the ordered binary tree.

2.put () method

The map interface defines the put method for storing elements in the map:

-V put (K key,v value)

If the key is already contained in the collection, it will replace the value of the key, and the return value will be the corresponding value of the key (or null if not key-value).

3.get () method

The Get method for getting elements from a map is defined in the map interface:

-V Get (Object Key)

Returns the value object corresponding to the parameter key, or null if it does not exist.

4.ContainsKey () method

The map interface defines whether a key exists in the map:

-boolean ContainsKey (Object key);

Returns TRUE if the map contains the given key, otherwise false.

5.Hashcode Method

From the HashMap principle we can see that the return value of the Hashcode () method of key plays an important role in hashmap storing elements. The Hashcode method is actually defined in object. Then the method should be properly rewritten;

For objects that override the Equals method, it is generally appropriate to rewrite the Hashcode method that inherits from the object class (the Hashcode method provided by object returns the integer form of the memory address where the object resides).

Overriding the Hashcode method is two points: one, the consistency with the Equals method, which equals the two object whose Hashcode () method returns True, and the return value of the hashcode should be the same; Imagine that if the return value of the Hashcode method for many objects is the same, the efficiency of the hash table will be greatly reduced, and the Hashcode method can typically be generated automatically using the tools provided by the IDE (such as Eclipse).

6. Loading factor and HashMap optimization.

Capacity: capacity, the number of buckets (barrels) in the hash table, that is, the hash array size.

Initial capacity: initial capacity, when creating a hash table, the number of initial buckets, the default build capacity is 16, or you can use a specific capacity.

Size: The amount of data that is stored in the current hash table.

Load factor: Load factor, default value 0.75 (that is, 75%), when adding data to the hash table if

Use of 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.