Since HashSet is based on HashMap, understanding HashMap is equivalent to understanding the HashSet, so this article is not the source of
- The key values are present, which is a new objcet
- PUT Operation:
return map.put(e,PRESENT) == null
- Remove Operation:
return map.remove(o) == PRESENT
- Traverse
Set.iterator
Set.toarray
- The put () method of Map returns NULL when a new key is added, and the old value is returned when a value associated with a key that already exists is updated. Thus the Add () method in Set can be used to determine whether the newly added element has changed the set, and returns True if it is changed. Thus PRESENT is not allowed to use NULL.
- The key value of map can be null, so hashset can also contain null values
- HashSet, Linkedhashset, TreeSet are not thread-safe, in the multi-threaded environment to pay attention to synchronization problems.
Copyonwritearrayset is a thread-safe implementation, but not a Map-based implementation, but a copyonwritearraylist implementation. Use the Addifabsent () method for de-weight, the performance is more general.
- The interior of the set is usually based on a map, and the Key in the map forms the set, and Value uses a meaningless Object. The feature of set is consistent with its internal set. HashSet based on HashMap is the best common implementation when unordered, Linkedhashmap-based Linkedhashset preserves the order of insertions or accesses, TreeMap-based TreeSet can be sorted by element ascending, requiring elements to be implemented COMAPR Able interface or a custom comparer.
?
HashSet of the Java collection