The set collection does not allow the same elements to be included, and if an attempt is made to add two identical elements to the same set set, the addition operation fails, and the Append () method returns False, and the new element is not added.
Features of the HashSet class:
- The order of elements cannot be guaranteed, and the order may differ from the order in which they are added, and may vary.
- HashSet is not synchronous and must be synchronized if multiple threads access and modify a hashset at the same time.
- The element value can be null.
Features of the Linkedhashset class:
is a subclass of the HashSet class that determines where an element is stored based on its hashcode value, but maintains the order of the elements using the linked list.
Features of the TreeSet class:
is an implementation class for the SortedSet interface, which ensures that the collection elements are in a sorted state.
Features of the Enumset class:
is a collection class designed for enumeration classes, where all elements must be enumerated values of the specified enumeration type.
The collection elements are ordered to determine the order of the collection elements in the order in which the enumeration values are defined within the enum class.
Inserting a null element is not allowed, otherwise it throws nullpointerexception.
Performance analysis of each set implementation class:
HashSet performance is always better than treeset (especially the most commonly used additions, query elements, and so on).
Linkedhashset is slightly slower than hashset, due to the extra overhead of maintaining the list.
Enumset is the best performance in any set implementation class, but it can only hold an enumeration value of the same enumeration class as a collection element
The set's three implementation classes HashSet, TreeSet, enumset are not thread-safe, and can be "wrapped" by the Synchronizedsortedset method of the Collections tool class. This is best done at creation time, in case of unintended non-synchronous access to the set collection.
Java Collection Class Learning notes (Set collection)