1.java Set Frame chart
2. Owning Package
Package java.util;
3. Inheritance and implementation relationships
public class hashset<e>
extends abstractset<e>
implements Set<e>, Cloneable, Java.io.Serializable
4. Preparatory work
A class hashset is actually an instance of a hashmap that does not guarantee the iteration order of the collection, nor does it guarantee that the order is immutable, allowing the use of NULL elements. Suggest first look HashMap source analysis: Java set of HashMap source code analysis
5. Property
Supported by a hash table (which is actually a HashMap instance).
private transient hashmap<e,object> map;
/* Because HashSet is a single value operation, and HashMap is the form of key-value pairs, because the HashSet element value as the HashMap key, then the corresponding value is present.
* SATISFY HASHMAP structure * *
private static final Object PRESENT = new Object ();
6. Construction Method
/** Construction Method 1:
* Construct an initialization capacity of 16, load factor of 0.75 HashMap/public
HashSet () {
map = new hashmap<> ();
}
/** Method 2:
* public
hashset (collection< extends e> c) {
map = new, using the HashMap set parameter as the parameter of the construction method * Hashmap<> (Math.max (int) (C.size ()/.75f) + 1));
AddAll (c);
}
public boolean addall (collection<? extends e> c) {
Boolean modified = false;
For (e e:c)
if (Add (e))
modified = true;
return modified;
}
/** Construction Method 3:
* Enter an initialization capacity and load factor construction method HashMap
*/public
hashset (int initialcapacity, float loadfactor) {
map = new Hashmap<> (initialcapacity, loadfactor);
}
/** Construction Method 4:
* Construct method for input initialization capacity HASHMAP/
public
hashset (int initialcapacity) {
map = new Hashmap<> ( initialcapacity);
}
7. Methods
Add Method:
/**
* Using the HashMap put method, if the added element already exists in HashMap, return True, otherwise return false
*
/public boolean Add (e) {
returns Map.put (E, PRESENT) ==null;
Remove method:
/**
* Using the Remove method in the HashMap, the value of the corresponding entry element is returned in the HashMap, and the value is compared with the present value stored.
* If the value is the same the deletion succeeds, otherwise the failure
/public boolean remove (Object o) {return
map.remove (o) ==present;
}
-----------------------------The source is jdk1.7 version of the