A vulgar implementation: Hashset

xiaoxiao2021-03-06  61

We know that HashMap can be seen as three views: key's set, value of the Collection, entry's set. Here, HashSet is actually a view of Hashmap, because Hashset uses HashMap, so it is very simple, this class is actually an example using hashmap, which is not worth analysis. Because you don't have to use the Value collection of HashMap, all Value is the same value (a useless object, but not null), this is not reasonable here. If you use null, the add and remove methods may have problems, because HashMap's PUT and REMOVE methods If returns null, we cannot determine whether there is an increase or delete (there is no return NULL, there is a return value value).

About HashMap: http: //blog.9cbs.net/treeroot/archive/2004/09/20/111104.aspxHashSet the following statement: public class HashSet extends AbstractSet implements Set, Cloneable, java.io.Serializable about AbstractSet: http: //blog.9cbs.net/treeroot/archive/2004/09/21/112219.aspx About Set: http://blog.9cbs.net/treeroot/archive/2004/09/18/109065.aspx about Cloneable: http://blog.9cbs.net/treeroot/archive/2004/09/07/96936.aspx

STATIC FIRIALSONUID = -5024744406713321676L;

Private Transient HashMap Map; // Hashset directly quotes a HashMap

Private static firm Object present = new object (); // As the value value of HashMap, there is no use.

Public HashSet () {map = new hashmap ();

Public Hashset (Collection C) {Map = New HashMap (Math.max ((int) (c.size () /. 75f) 1, 16)); addall (c);}

Public Hashset (Int Initialcapact) {Map = New Hashmap (Initialcapacity, LoadFactor);

Public hashset (int initialcapacity) {map = new hashmap (initialcapacity);

Hashset (int initialcapacity, boolean dummy) {map = new linkedhashmap (Initialcapacity, LoadFactor);

Public iterator itrator () {return map.keyset (). iterator ();

Public int size () {return map.size ();

Public Boolean ISempty () {Return Map.Isempty ();

Public Boolean Contains (Object O) {Return Map.Containskey (O);

Public Boolean Add (Object O) {Return map.put (o, present) == null;} public boolean remove (Object O) {return map.remove (o) == present;

Public void clear () {map.clear ();

Public Object Clone () {Try {Hashset Newset = (Hashset) Super.clone (); newset.map = (hashmap) map.clone (); return newset;} catch (clonenotsuptedexception e) {throw new internelrror ();} }

private synchronized void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException {s.defaultWriteObject (); Write out HashMap capacity and load factor s.writeInt (map.capacity ()); s.writeFloat (map.loadFactor ()); S.writeint (map.size ()); for (Iterator i = map.keyset (). Iterator (); I.hasnext ();) S.WriteObject (i.next ());}

private synchronized void readObject (java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {s.defaultReadObject (); int capacity = s.readInt (); float loadFactor = s.readFloat (); map = (this instanceof LINKEDHASHSET? New LinkedHashmap (Capacity, LoadFactor): New HashMap (Capacity, LoadFactor); int size = s oredint (); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-114526.html

New Post(0)