Java Learning (Map interface)

Source: Internet
Author: User

First, overview:

By looking at the map interface description, we discover the collection under the map interface and the set under the collection interface, which store the data in different forms , such as.

1. collections in Collection , where elements are isolated (understood as single), are stored in the same way as elements stored in the collection.

2. The collection in Map , the element is a pair of existence (understood as husband and wife). Each element consists of a key and a value of two , and the corresponding value can be found by the key .

3. The collection in Collection is called a single-column collection , and the collection inMap is called a double-row collection .

4. It is important to note that the collection in map cannot contain duplicate keys , values can be duplicated , and each key can only correspond to one value . a value can correspond to more than one key .

5. The collections commonly used in map are HashMap collections ,linkedhashmap collections .

Example diagram:

Overview of common collections in map interfaces

Common HashMap collections ,linkedhashmap Collections

1. hashmap<k,v>: The hash table structure of the stored data, the access order of the elements is not guaranteed to be consistent . The hashcode () method, Equals () method, which requires overriding The key , is required to ensure that the key is unique and non-repeatable.

2. linkedhashmap<k,v>: HashMap has sub-class Linkedhashmap, storing 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 Hashcode () method, the Equals () method, which can guarantee the uniqueness and non-repetition of keys through the hash table structure.

Note : The collection in the map interface has two generic variable <k,v> , which, when used , assigns a data type to two generic variables . The data type of the two generic variables <K,V> can be the same or different .

Three, the common method of map interface

1.put method : Corresponds the specified key to the value and is added to the collection

Method returns the value that corresponds to the key

When using the Put method, if the specified key (key) is not in the collection , there is no value corresponding to the key, returns null, and adds the specified key value to the collection ;

When using the Put method, 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 the The value corresponding to the specified key , replaced by the specified new value .

2.Get method : Gets The value that corresponds to the specified key (key)

3. Removemethod : Deletes an element based on the specified key (key) , returning the value of the deleted element (value)

Instance:

 Public Static voidmethod1 () {//creating a key-value collection ObjectMap<string,string> hm=NewHashmap<string,string>(); //adding elementsHm.put ("Huang Xiaoming", "Baby"); Hm.put ("Deng Chao", "Sun Li"); Hm.put ("Li Chen", "Fan Bingbing"); Hm.put ("Xu Zheng Does", "actorceramic"); //Print pre-overwrite valueSystem.out.println (Hm.put ("Xu Zheng Does", "Huang Bo")); //Delete Element//hm.remove ("Xu Zheng does"); //key is the same, the previous value is overwritten//hm.put ("Xu Zheng Does", "Huang Bo");System.out.println (Hm.get ("Xu Zheng Does")); }

Four, Map set traversal key Find value way

key to find the value: that is, by the key in the element, the value corresponding to the key is obtained

Operating Procedures and illustrations:

1. Gets all the keys in the map collection, because the keys are unique , so return a set set to store all the keys

2. iterate through the set of keys to get each key

3. According to the key, get the value corresponding to the key

Graphic:

Code Demo:

 Public Static voidmethod2 () {//Traverse key to find valueMap<string,string> hm=NewHashmap<string,string>(); //adding elementsHm.put ("Huang Xiaoming", "Baby"); Hm.put ("Deng Chao", "Sun Li"); Hm.put ("Li Chen", "Fan Bingbing"); Hm.put ("Xu Zheng Does", "actorceramic"); //Traverse Map//idea: 1, first get the set set of all keys//2. Iterate through the set collection to get each key//3. Call the Get method according to the key to get each value//1. Get the set set of all keys firstSet<string> set=Hm.keyset (); //2. Traversing set setIterator<string> it=Set.iterator ();  while(It.hasnext ()) {//3. Call the Get method according to the key to get each valueString key=It.next (); String St=Hm.get (key); SYSTEM.OUT.PRINTLN (Key+".." +St); }    }

V. Entry key-value pair object

In the map class design, a nested interface is provided: Entry. entry encapsulates the corresponding relationship of a key-value pair into an object . That is, the key-value pair object , so that when we traverse the map collection , we can get the corresponding key and corresponding value from each key-value pair (Entry) object.

Entry is a static internal nested interface provided in the map interface

Methods of entry:

1.GetKey () method : Gets the key in the entry object

2.getValue () method : Gets the value in the Entry object

EntrySet () method : Used to return all key-value pairs (Entry) objects in the Map collection, returned as a set set

Six, Map set traversal key value pair mode

Key-value pairs: gets the keys and values in a key-value pair (Entry) object through each key-value pair (Entry) object in the collection.

Operating Procedures and illustrations:

1. Gets all the key-value pairs (Entry) objects in the Map collection, returned as a set collection.

 2. Traverse the set set containing the key-value pairs (Entry) object to get each key-value pair (Entry) object

3. Gets the key and value in the Entry object by using a key-value pair (Entry) object.

Note: The Map collection cannot be traversed directly using iterators or foreach . But you can use it after you turn it into a set .

Graphic:

Code demo;

 Public Static voidmethod4 () {Map<String,String> map=NewHashmap<string,string>(); Map.put ("Bu", "West Cool Army"); Map.put ("Zhao Yun", "Shu army"); Map.put ("Lu Xun", "WU"); Map.put ("Zhuge Liang", "Shu army"); //Traverse Map//idea: 1. Get a set set of marriage certificates//2. Walk through marriage certificate collection get every marriage certificate//3. Get each marriage certificate after obtaining the husband's name and the woman's name in the marriage certificateSet<map.entry<string, string>> set=Map.entryset ();
     //Enhanced for loop traversal
//Traverse marriage Certificate//for (map.entry<string, string> entry:set) {// //get the name of a marriage certificate//String Key=entry.getkey ();// //get a woman's name for a marriage certificate//String Value=entry.getvalue ();//System.out.println (key+ "..." +value);// }
Iterator Traversal
Iterator<map.entry<string, string>> it=Set.iterator (); while(It.hasnext ()) {Map.entry<String,String> me=It.next (); String Key=Me.getkey (); String value=Me.getvalue (); SYSTEM.OUT.PRINTLN (Key+".." +value); } }

Java Learning (Map interface)

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.