Java Map a key can actually save multiple values

Source: Internet
Author: User

The Map we usually use can only store the same Key in the Map. The same key we save later will overwrite the original key value, as shown in the following example.

[Java]
<Pre class = "java" name = "code"> package test62;
 
Import java. util. HashMap;
Import java. util. Map;
Import java. util. Map. Entry;
 
Public class test {
 
/**
* @ Param args
* @ Author Wang Xin
*/
Public static void main (String [] args ){
 
String str1 = new String ("xx ");
String str2 = new String ("xx ");
System. out. println (str1 = str2 );

Map <String, String> map = new HashMap <String, String> ();
Map. put (str1, "hello ");
Map. put (str2, "world ");

For (Entry <String, String> entry: map. entrySet ())
{
System. out. println (entry. getKey () + "" + entry. getValue ());
}
System. out. println ("---->" + map. get ("xx "));
}
 
}


In this example, we can see that only one value can be saved for the same key. Next we can see that a map can save multiple values in one key. This map is also the IdentityHashMap. Next we will introduce the use of the IdentityHashMap class.

The API explains this class as follows: This class is not a general Map implementation! When this type of Map interface is implemented, it intentionally violates the conventional Map protocol, which forces the equals method when comparing objects. This design is only used in rare cases where equality semantics needs to be referenced.

The IdentityHashMap class uses a hash table to implement the Map interface. When comparing keys (and values), it uses reference equality to replace object equality. Let's take a look at the code of this class:

[Java]
Package test62;
 
Import java. util. IdentityHashMap;
Import java. util. Map;
Import java. util. Map. Entry;
 
Public class test1 {
Public static void main (String [] args ){

String str1 = "xx ";
String str2 = "xx ";
System. out. println (str1 = str2 );

Map <String, String> map = new IdentityHashMap <String, String> ();

Map. put (str1, "hello ");
Map. put (str2, "world ");

For (Entry <String, String> entry: map. entrySet ())
{
System. out. println (entry. getKey () + "" + entry. getValue ());
}
System. out. println ("containsKey --->" + map. containsKey ("xx "));
System. out. println ("value ---->" + map. get ("xx "));
}
}

The output result of this Code is as follows:

True
Xx world
ContainsKey ---> true
Value ----> world

Why does our Key save only one value ???? This question is explained in this way in the same way as in java 62nd. Let's take a look:

The language specification ensures that strings are limited by memory. In other words, Equal string constants are also the same [JLS 15.28]. This ensures that the String literal constant "xx" that appears for the second time in our program references the same String instance as the first time, therefore, although we use IdentityHashMap to replace Map implementation for general purposes such as HashMap, it will not affect the program behavior.

Let's take a look at the code below to implement a key to save two values. The Code is as follows:

[Java]
<Pre class = "java" name = "code"> package test62;
 
Import java. util. IdentityHashMap;
Import java. util. Map;
Import java. util. Map. Entry;
 
Public class test1 {
Public static void main (String [] args ){

String str1 = new String ("xx ");
String str2 = new String ("xx ");
System. out. println (str1 = str2 );

Map <String, String> map = new IdentityHashMap <String, String> ();
Map. put (str1, "hello ");
Map. put (str2, "world ");


For (Entry <String, String> entry: map. entrySet ())
{
System. out. println (entry. getKey () + "" + entry. getValue ());
}
System. out. println ("containsKey --->" + map. containsKey ("xx "));
System. out. println ("str1 containsKey --->" + map. containsKey (str1 ));
System. out. println ("str2 containsKey --->" + map. containsKey (str2 ));
System. out. println ("value ---->" + map. get ("xx "));
System. out. println ("str1 value ---->" + map. get (str1 ));
System. out. println ("str2 value ---->" + map. get (str2 ));
}
}


The output result is as follows:
False
Xx world
Xx hello
ContainsKey ---> false
Str1 containsKey ---> true
Str2 containsKey ---> true
Value ----> null
Str1 value ----> hello
Str2 value ----> world

We can know that IdentityHashMap depends on objects to determine whether keys are equal. If we need to save multiple values for a key, we need to use this IdentityHashMap class, in this way, we can use this class as needed.


Author: wx_962464

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.