[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.