Last Updated: 27 August, 2023
The Set interface in Java is part of the Java Collections Framework. It extends the Collection interface. Set was introduced in the JDK 1.2 version and is available in the java.util package.
The Set Interface is an unordered collection of objects that does not allow the insertion of duplicate elements into the Set.
The Set interface inherits the Collection interface's methods and adds a feature that restricts the insertion of duplicate elements.
The set interface is used to create the mathematical set.
In the above diagram, the Set interface extends the java.util.Collection interface. The Set interface is implemented by the HashSet, LinkedHashSet, and TreeSet classes. There are two interfaces that extend the Set implementation, namely SortedSet and NavigableSet.
Here, E defines the type of elements that the Set will contain.
The Set interface is implemented by the HashSet, LinkedHashSet, and TreeSet classes in Java.
The HashSet class is a member of the Java Collection Framework. HashSet was introduced in the JDK 1.2 version and is available in the java.util package.
A HashSet is used to create an unordered collection of elements that contains only unique elements; no duplicate elements are allowed. The HashSet provides constant-time performance for general operations such as add, remove, contain, size, etc.
HashSet's internal implementation is based on the hash table for creating and storing a collection of unique elements.
HashSet Class Declaration
Here, E defines the type of elements that the Set will contain.
Read more →The LinkedHashSet class of the Java collections framework was introduced in the JDK 1.4 version and is available in the java.util package.
LinkedHashSet is the Hashtable and linked list implementation of the Set interface with preserved iteration order. The linked list defines the iteration order, which is the order in which elements are inserted into the set. Insertion order is not affected if an element is re-inserted into the set.
LinkedHashSet is a child class of HashSet, so it has all the functionalities of the HashSet class. The HashSet does not maintain the insertion order of elements, whereas the LinkedHashSet maintains the element insertion order. This one is the main difference between both.
LinkedHashSet Class Desclaration
Here, E defines the type of elements that the Set will contain.
Read more →The TreeSet class of the Java collections framework was introduced in the JDK 1.2 version and is available in the java.util package.
A TreeSet is a sorted collection of objects. A TreeSet is the implementation of the SortedSet interface in Java that uses a tree for storage. The ordering of the elements is maintained by a set using their natural (ascending) order.
TreeSet provides fast access and retrieval operations, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.
TreeSet Class Desclaration
Here, E defines the type of elements that the Set will contain.
Read more →Please note that Set is an interface, we can't directly create an instance of it; we need to use one of its implementing classes like HashSet, LinkedHashSet, or TreeSet to create actual sets.
Output
Elements: [800, null, 500, 200]
There are two interfaces that extend the Set Interface implementation.
SortedSet is an interface in the Java Collections Framework that extends the Set interface. Sorted was introduced in the JDK 1.2 version and is available in the java.util package.
It maintains a collection of elements in ascending order, sorted according to the elements' natural ordering or according to a Comparator provided at SortedSet creation time.
SortedSet Class Desclaration
Here, E defines the type of elements that the Set will contain.
Read more →In Java, NavigableSet is an interface that extends the SortedSet interface and provides additional navigation methods for accessing elements in a sorted set. It is part of the Java Collections Framework and is available from Java 6 onward.
A NavigableSet maintains a sorted order of its elements, and it allows efficient retrieval of elements based on their relative position in the set. It provides methods to navigate through the set in both ascending and descending order, perform range queries, and find closest matches for a given value.
NavigableSet Class Desclaration
Here, E defines the type of elements that the Set will contain.
Read more →A set interface includes the following important methods: add, remove, clear, size, etc., to facilitate its use within the collections framework. They have been listed in the following table:
Method | Description |
---|---|
boolean add(E e) | Adds the specified element to the set if it is not already present. If the set already contains the element, the call leaves the set unchanged and returns false. |
boolean addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection to the set if they are not already present. |
void clear() | Removes all of the elements from the set |
boolean contains(Object o) | Returns true if the set contains the specified element. |
boolean containsAll(Collection<?> c) | Returns true if the set contains all of the elements of the specified collection. |
boolean equals(Object o) | Compares the specified object with the set for equality. |
int hashCode() | Returns the hash code value for this set. |
boolean isEmpty() | Returns true if the set contains no elements. |
Iterator | Returns an iterator over the elements in the set. |
boolean remove(Object o) | Removes the specified element from this set if it is present. |
boolean removeAll(Collection<?> c) | Removes from the set all of its elements that are contained in the specified collection. |
boolean retainAll(Collection<?> c) | Retains only the elements in the set that are contained in the specified collection. |
int size() | Returns the number of elements in the set (its cardinality). |
Object[] toArray() | Returns an array containing all of the elements in the set. |
<T> T[] toArray(T[] a) | Returns an array containing all of the elements in the set; the runtime type of the returned array is that of the specified array. |
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Set.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between the List and Set interfaces in Java?
Ans. List and Set both interfaces are available in the
List | Set |
---|---|
A List is an ordered collection of elements. | A Set is an unordered collection of elements. |
List contains duplicate elements. | Set does not contain duplicate elements. |
List accepts any number of nulls as an element. | Set accepts only one null as an element. |
List allows positional access to elements. | Set does not allow positional access to elements. |
List has one legacy class called Vector. | Set does not have any legacy classes. |
List uses ListIterator for traversing the elements in any direction. | ListIterator does not work with Set. |
List implementation classes are ArrayList, LinkedList, Vector and Stack. | Set implementation classes are TreeSet, HashSet and LinkedHashSet. |
Is it allowed to insert a null value in a Set?
Ans. In set implementations like HashSet and LinkedHashSet, however, a single null value is allowed. If null is input to TreeSet, it throws a runtime exception.
Is Set an ordered collection in Java?
Ans. No, Set is not an ordered collection in Java. It does not provide positional access either.
Is Java Set iterable?
Ans. Yes. The set interface implements an Iterable interface and thus set can be traversed or iterated using a forEach loop.