HashSet implements the set interface, serializable, cloneable interface. Duplicate values are not allowed, but can be null and are not guaranteed to be consistent with the insertion order.
The time of the hashset iteration is proportional to the size of the HashSet instance (the number of elements) and the "capacity" of the underlying HashSet instance (the number of buckets). Therefore, if iterative performance is important, do not set the initial capacity too low and the load factor is too small. When the number of elements in the hash table is greater than the product of the current capacity and load factor, the expansion is about twice times the current capacity.
Where the data object must implement the Hashcode () function, the hashset thread is out of sync, using the Collections.synchronizedset wrapper.
constructor function:
HashSet () default initial size 16, reprint factor 0.75
HashSet (int initialcapacity)
HashSet (int initialcapacity,float loadfactor)
HashSet (Collection C)
Common methods:
Boolean Add (E E)
void Clear ()
Boolean contains (Object o)
Boolean remove (Object o)
Example:
Package test;
import java.util.*;
/**
* @author Administrator
*
*/
Public class Testhashset {
Public Static void Main (String args[]) {
hashset<string> hash=New hashset<string> ();
linkedlist<string> list=New linkedlist<string> ();
List.add ("List1");
List.add ("List2");
List.add ("List3");
Hash.add ("hash Set1");
Hash.add ("Hash Set2");
Hash.add ("hash Set3");
Hash.add ("hash Set4");
Hash.add ("hash Set5");
Hash.add ("hash Set6");
Hash.addall (list);//The list added to the hash set is not present as a separate element, [List1, Hash Set6, LIST3, List2, hash set3, hash set2, hash set5, hash set4, hash Set1]
System. out. println (hash);
Hash.remove ("hash Set3");
System. out. println (hash);
Iterator<String> it= hash.iterator ();
while (It.hasnext ()) {
System. out. println (It.next ());
}
}
}
HashSet is actually built on the basis of HashMap. The map's key is the set element, and the map value is constant.
Public Boolean Add (e e) {
return map. Put (E,PRESENT) = = null;
}
HashSet Hash set base for Java collections