2017-10-31 19:20:45
Set: unordered and unique
Implementing subclasses:HashSet,
This class implements the Set interface, which is supported by a hash table ( actually a HashMap instance ). It does not guarantee the set's iteration order, especially it does not guarantee that the order is constant. This class allows the use of null elements.
Note that this implementation is not synchronous. If multiple threads access a hash set at the same time, and at least one of the threads modifies the set, it must remain externally synchronized.
HashSet the underlying data structure is a hash table (HASHMAP), the hash table relies on the hash value of the storage, the addition of functional bottom-dependent two methods: int Hashcode (), Boolean equals (Object obj).
* Construction Method
* Common Methods
Explanation of the uniqueness of the HashSet:
SOURCE anatomy: Add a functional bottom-up dependency two methods: int Hashcode (), Boolean equals (Object obj)
HashSet class HashSet implements set{private static final Object PRESENT = new Object (); private transient hashmap<e,object> map; Constructs a method that returns a HashMap public HashSet () {map = new hashmap<> (); }//add method Public boolean Add (e e) {return Map.put (E, PRESENT) ==null; }}//hashmap class HashMap implements map{public V put (K key, V value) {return Putval (hash (key), key, value, Fals E, true); } final V putval (int hash, K key, V value, Boolean onlyifabsent, Boolean evict) {Node<k,v> ; [] tab; Node<k,v> p; int n, I; if (tab = table) = = NULL | | (n = tab.length) = = 0) n = (tab = resize ()). length; if (p = tab[i = (n-1) & hash]) = = null) tab[i] = NewNode (hash, key, value, NULL); else {node<k,v> E; K K; if (P.hash = = Hash && (k = p.key) = = Key | | (Key! = null && key.equals (k)))) e = p; else if (P instanceof TreeNode) e = ((treenode<k,v>) p). puttreeval (This, tab, hash, key, value) ; else {for (int bincount = 0;; ++bincount) {if ((E = p.next) = = null) { P.next = NewNode (hash, key, value, NULL); if (bincount >= treeify_threshold-1)//-1 for 1st treeifybin (tab, hash); Break } if (E.hash = = Hash && (k = e.key) = = Key | | (Key! = null && key.equals (k)))) Break p = e; }} if (E! = null) {//Existing mapping for key V oldValue = E.value; if (!onlyifabsent | | oldValue = = NULL) E.value = value; Afternodeaccess (e); return oldValue; }} ++modcount; if (++size > Threshold) resize (); Afternodeinsertion (evict); return null; }}
An example problem, when adding custom objects, the values of the two classes are equal, but will still be judged as different elements, because there is no overriding hashcode (), so the default is to invoke the hashcode () of the object class, and the hashcode of the same class is generally different.
hashset<student> HashSet = new hashset<> (); Student S1 = new Student ("Liu Yifei", +); Student s2 = new Student ("Zhang Ziyi", +); Student s3 = new Student ("Liu Yifei", +); Hashset.add (S1); Hashset.add (S2); Hashset.add (S3); System.out.println (HashSet);
The workaround is to rewrite the hashcode () and Equals () Methods yourself:
public class Student { private String name; Private Integer age; Student (String name,int age) { this.name=name; this.age=age; } @Override public int hashcode () { //return 0;
Return This.name.hashCode () +this.age*11; } @Override public boolean equals (Object obj) { if (this = = obj) return true; if (! ( obj instanceof Student)) return false; Student s = (Student) obj; Return This.name.equals (s.name) && this.age.equals (s.age); }}
Here we use the instanceof operator, this operator and = =, >= is the same nature, but is described in English, is a two-dollar operator, used to determine whether the left side of this particular class or is an instance of its subclass.
Java Collection-set interface and its subclasses