[Java class set] _ identityhashmap class notes

Source: Internet
Author: User

[Java class set] _ identityhashmap class notes

In normal map operations, the key itself cannot be repeated.

Import Java. util. map; import Java. util. hashmap; import Java. util. iterator; import Java. util. set; class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age;} public Boolean equals (Object OBJ) {If (this = OBJ) {return true;} If (! (OBJ instanceof person) {return false;} person per = (person) OBJ; If (this. name. equals (Per. name) & this. age = per. age) {return true;} else {return false;} public int hashcode () {return this. name. hashcode () * This. age ;}} public class hashmapdemo08 {public static void main (string ARGs []) {Map <person, string> map = NULL; Map = new hashmap <person, string> (); map. put (new person ("James", 30), "zhangsan_1"); // Add content map. put (new person ("Zhang San", 30), "zhangsan_2"); // Add content map. put (new person ("Li Si", 31), "Lisi"); // Add content set <map. entry <person, string> allset = NULL; allset = map. entryset (); iterator <map. entry <person, string> iter = NULL; iter = allset. iterator (); While (ITER. hasnext () {map. entry <person, string> me = ITER. next (); system. out. println (Me. getkey () + "-->" + me. getvalue ());}}}

Output:
Name: Li Si; age: 31 --> Lisi
Name: Zhang San; age: 30 --> zhangsan_2

When hashmap is used for operations, the key content cannot be repeated. If you want to repeat the key content, the two objects have different addresses. key1 = key2) use the identityhashmap class.

Import Java. util. map; import Java. util. identityhashmap; import Java. util. iterator; import Java. util. set; class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age;} public Boolean equals (Object OBJ) {If (this = OBJ) {return true;} If (! (OBJ instanceof person) {return false;} person per = (person) OBJ; If (this. name. equals (Per. name) & this. age = per. age) {return true;} else {return false;} public int hashcode () {return this. name. hashcode () * This. age ;}} public class identityhashmapdemo01 {public static void main (string ARGs []) {Map <person, string> map = NULL; Map = new identityhashmap <person, string> (); map. put (new person ("James", 30), "zhangsan_1"); // Add content map. put (new person ("Zhang San", 30), "zhangsan_2"); // Add content map. put (new person ("Li Si", 31), "Lisi"); // Add content set <map. entry <person, string> allset = NULL; allset = map. entryset (); iterator <map. entry <person, string> iter = NULL; iter = allset. iterator (); While (ITER. hasnext () {map. entry <person, string> me = ITER. next (); system. out. println (Me. getkey () + "-->" + me. getvalue ());}}}

Output:
Name: Zhang San; age: 30 --> zhangsan_1
Name: Zhang San; age: 30 --> zhangsan_2
Name: Li Si; age: 31 --> Lisi

Even if the content of the two objects is equal, but because the new keyword is used, the address must not be equal, so you can add it, and the key can be repeated.

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.