Java's ''''''interface defines the Set. A Set can't have any duplicate elements in it. Additionally, the Set has no set order. As such, elements can't be found by index. Set is implemented by '
, , and '.
interface implementations There are several implementations of the Set interface, including and its subclasses, and the final static inner class (where and are formal type parameters). ======== is a
skeletal implementation for the interface. Direct subclasses of include , , , and .
class The class extends . The class has no public constructors, and only contain static factory methods. contains the static factory method . This method is an aggregation method. It takes in several parameters, takes into account of the type of the parameters, then returns an instance with the appropriate type. As of 2018, In Java SE8 OpenJDK implementation uses two implementations of which are invisible to the client, which are and . If the no longer provided any performance benefits for small enum types, it could be removed from the library without negatively impacting the Java Collection Library. is a good replacement for the
bit fields, which is a type of set, as described below. Traditionally, whenever developers encountered elements of an enumerated type that needs to be placed in a set, the developer would use the
int enum pattern in which every constant is assigned a different power of 2. This bit representation enables the developer to use the bitwise OR operation, so that the constants can be combined into a set, also known as a
bit field. This
bit field representation enables the developer to make efficient set-based operations and bitwise arithmetic such as intersection and unions. However, there are many problems with
bit field representation approach. A bit field is less readable than an int enum constant. Also, if the elements are represented by bit fields, it is impossible to iterate through all of these elements. A recommended alternative approach is to use an , where an int enum is used instead of a
bit field. This approach uses an to represent the set of values that belong to the same type. Since the implements the interface and no longer requires the use of bit-wise operations, this approach is more type-safe. Furthermore, there are many static factories that allow for object instantiation, such as the method method. After the introduction of the , the
bit field representation approach is considered to be obsolete.
class HashSet uses a hash table. More specifically, it uses a '''''' to store the hashes and elements and to prevent duplicates.
class The java.util.LinkedHashSet class extends by creating a doubly linked list that links all of the elements by their insertion order. This ensures that the iteration order over the Set is predictable.
class is a concurrent replacement for a synchronized . It provides improved concurrency in many situations by removing the need to perform synchronization or making a copy of the object during iteration, similar to how acts as the concurrent replacement for a synchronized . On the other hand, similar to , should not be used when synchronization is mandatory.
interface The java.util.SortedSet interface extends the java.util.Set interface. Unlike a regular Set, the elements in a SortedSet are sorted, either by the element's method, or a method provided to the constructor of the SortedSet. The first and last elements of the SortedSet can be retrieved using the and methods respectively, and subsets can be created via minimum and maximum values, as well as beginning or ending at the beginning or ending of the SortedSet. The java.util.TreeSet class implements the SortedSet interface.
interface The '''''' interface extends the java.util.SortedSet interface and has a few additional methods. The , , , and methods find an element in the set that's close to the parameter. Additionally, a descending iterator over the items in the Set is provided. As with SortedSet, java.util.TreeSet implements NavigableSet.
class java.util.TreeSet uses a
red–black tree implemented by a '
. The red–black tree ensures that there are no duplicates. Additionally, it allows TreeSet to implement '.
class acts as a concurrent replacement for implementations of a synchronized . For example it replaces a that has been wrapped by the method. == interfaces==