Usage Scenarios for Java map

Source: Internet
Author: User

Tag:remove    Replace    map collection     features     non-repetition     entry   nbsp   linked   dhash   

Summary of usage scenarios for map interfaces (DAY04)

MAP:
A collection of maps in which elements are paired (understood as couples). Each element consists of a key and a value, which can be used to find the corresponding value
A collection in map cannot contain duplicate keys, values can be duplicated, and each key can only correspond to one value.

Characteristics:
Keys do not repeat, values can be repeated
Each key can only correspond to one value

Sub-class:
HASHMAP:
The hash table structure is used to store the data, and the access order of the elements cannot be guaranteed. Because the key is guaranteed to be unique and non-repeatable,
The Hashcode () method, Equals () method, which requires overriding the key.
Linkedhashmap
HashMap has sub-class Linkedhashmap, which stores data using the hash table structure + linked list structure.
The structure of the linked list ensures that the access order of the elements is consistent;
The hash table structure guarantees that the keys are unique, not duplicated, that the Hashcode () method, the Equals () method, need to override the key.

Common methods:
-Public V put (K key, V value): Adds the specified key to the map collection with the specified value.
-Public V Remove (Object key): Removes the key value of the specified key against the element in the map collection, returning the value of the deleted element.
-Public V get (Object key) gets the corresponding value in the map collection according to the specified key.
-Public set<k> KeySet (): Gets all the keys in the map collection and stores them in the set collection.
-Public set<map.entry<k,v>> EntrySet (): Gets a collection of all the key-value pairs of objects in the Map collection (set collection).

Notes
When using the Put method, if the specified key (key) is not in the collection, there is no value corresponding to the key, and Null is returned.
and adds the specified key value to the collection;

If the specified key (key) exists in the collection, the return value is the value corresponding to the key in the collection (the value is the value before the substitution).
and replaces the value corresponding to the specified key with the specified new value.

Traversal method:
The Fist
Key to find the value: that is, by the key in the element, the value corresponding to the key is obtained
Analysis steps: With keyset ()
① gets all the keys in the map collection because the keys are unique, so returning a set set stores all the keys,
② Traversal of all keys
③ to find the corresponding value according to the key

The Scecond
Entry represents a pair of keys and values, it also provides the access to the corresponding key and the corresponding worthy method
-Public K GetKey (): Gets the key in the entry object.
-Public V GetValue (): Gets the value in the entry object.
A method for getting all entry objects is also available in the map collection:

-Public set<map.entry<k,v>> EntrySet (): Gets a collection of all the key-value pairs of objects in the Map collection (set collection).

① Gets the map collection, all the key values are returned as set for the entry object, EntrySet ()
② traversal of a collection of objects containing key-value pairs get each pair of key-value pairs of objects
③ to get key and value method hints GetKey () GetValue () from Entry object by key value pair Object


1.HashMap
①hashmap storing custom type key values
-When a custom object is stored in the HashMap, if the custom object exists as a key, the object is guaranteed to be unique,
The hashcode and Equals methods of the object must be replicated (if you forget, review hashset to store your custom objects).
-If you want to ensure that the keys stored in the map are in the same order as they are taken out, you can use the Java.util.LinkedHashMap collection to store them.
2LinkedHashMap
① Order of disordered HashMap

Usage Scenarios for Java 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.