We all know that. There are no repeating elements in map and set?
But we don't know about the internal details. Let's talk about it today!
1 for HashMap HashSet
The implementation of their underlying data structure is: maintenance of a HashTable. The elements in the container are all stored in the Hashtable. When they add elements again, how do they infer that there are repeated elements? Each element that is added has a hashcode (hash value), and they first compare the hash value, is it the same? Not the same element, join into HashTable. Assuming that Hashcode is the same, then the equivalent Equals () method, assuming the same thing, the JVM feels that the data already exists. No data will be added!
1:
2 for TreeMap TreeSet
The bottom of them is data structure implementation is: Maintenance of a binary tree. When adding elements to a container, how do they infer that they have the same element? We are all until TreeMap TreeSet they are stored in an orderly manner.
In order to maintain the uniqueness of the data. When the data is deposited, they invoke the comparable CompareTo () method implemented in the element (code 1). Or the collection itself is created by passing in an iterator (code 2). The detailed implementation is: Call the comparison method, return-1, add to the left subtree, return 1 when added to the right subtree.
Returns 0 has the same data not to join the element!
2
Code 1; (Principle i)
Package Stu.love.v;import java.util.*;//When to use map/* when there is a mapping relationship. Each student is given an address name, and the same age as the same person *///the object itself in the container is relatively comparative. Class Studentd implements comparable<studentd>{private string name;private int age;public studentd (string name, int age) {this.name = Name;this.age = age;} public int CompareTo (Studentd stu) {int t = This.age-stu.age;return t==0?This.name.compareTo (stu.name): t;} Overridden the Hashcode method public int hashcode () {return name.hashcode () +age*36;} Overrides the Equals method public boolean equals (Object obj) {if (! obj instanceof Studentd)) throw new ClassCastException ("type exception"); Studentd stu = (studentd) Obj;return this.name.equals (stu.name) && this.age ==stu.age;} public void SetName (String name) {this.name = name;} public void Setage (int.) {this.age = age;} Public String GetName () {return this.name;} public int getage () {return this.age;} Public String toString () {return this.name + "," +age;}} Class Demo16 {public static void main (string[] args) {//Guaranteed key only principle, first infer whether the hash value is the same, again infer equals () hashmap<studentd,string& Gt HM = new hashmap<studentd,string> (), Hm.put (New Studentd ("Xiaobai", "Shanghai"), Hm.put (New Studentd (" "Wanghei", "Beijing"), Hm.put (New Studentd ("Lisi", "Shenzhen"), Hm.put (New Studentd ("Lisi", "Shenzhen"); Map the first iteration method based on key to find valueset<studentd> set=hm.keyset (); for (iterator<studentd> ite = Set.iterator (); Ite.hasnext ();) {studentd stu = Ite.next (); String value = Hm.get (stu); SOP (stu+ "address is:" +value);} Another iteration of map gets a key-value pair. Entry gets the key and valueset<map.entry<studentd,string>> entry = Hm.entryset (); for (iterator<map.entry <StudentD,String>> ite = Entry.iterator (); Ite.hasnext ();) {map.entry<studentd,string> kv = Ite.next (); Studentd key = Kv.getkey (); String value = Kv.getvalue (); SOP (key+ "The address is:" +value);}} public static void Sop (Object obj) {System.out.println (obj);}}
Code Listing 2:
Package Stu.love.v;/*treemap:hashmap guarantee Key only principle and HashSet same TREEMAP guarantee key only principle and TreeSet same */import Java.util.*;class Student1{private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} private int age;public Student1 (String name,int age) {this.name = Name;this.age = age;} Public String toString () {return name+ "," +age;}} The comparison class Comparebyname implements Comparator<student1>{public int compare (Student1 s1,student1 s2) {//This is a good way to write!Concise int t = S1.getname (). CompareTo (S2.getname ()); return T ==0?s1.getage ()-s2.getage (): t;}} Class Demo17{public static void Main (string[] args) {//Principle two://Guaranteed key only principle: The return value of the comparison method is 0treemap<student1,string> TM = new Treemap<student1,string> (New Comparebyname ()), Tm.put (New Student1 ("Xiaobai", "Shanghai"); Tm.put (new Student1 ("Wanghei", "Beijing"), Tm.put (New Student1 ("Lisi"), "Shenzhen"), Tm.put (New Student1 ("Lisi", 28), " Shenzhen "); set<map.entry<student1,string>> Entry = Tm.entryset (); for (iterator<map.entry<student1,string >> it = Entry.iterator (), It.hasnext ();) {map.entry<student1,string> kv = It.next (); Student1 key = Kv.getkey (); String value = Kv.getvalue (); SOP (key+ "The address is:" +value);}} public static void Sop (Object obj) {System.out.println (obj);}}
How does the Java Map implement key uniqueness?