Excerpt from the station b is still Silicon Valley video tutorials JAVA
Set interface has three implementation class:
/ ---- HashSet: Set main interface implementation class; thread-safe; null value may be stored; bottom: an array list +
/ ---- LinkedSet: HashSet as a subclass of increased Link, such that when the traversal, in order traversal may be added to the frequent traversal more efficient.
/ ---- TreeSet: Use red-black tree implementation, can be customized sort of adding elements added element must be of the same class.
Add class must be comparable, there are two methods: Comparable and Comparator two ways.
Adding to the HashSet class, be sure to override the hashCode method and the equals method, and consistent as possible.
HashSet add a new element a:
By a hash algorithm to calculate a hash value, it is mapped onto the corresponding array index by some mapping;
1 if the index position no elements added;
If element 2 b, then the hash value comparison, if the hash values are different, then added;
3 If the hash values are the same, it equals method is invoked, if the return result is false, add, or else be added.
In 23 cases, if a successfully added elements:
JDK 7: into the array element a, element b point
JDK 8: element b unchanged, a points element