Java Set Interface
The Set interface is an important component of the Java Collections
Framework and is used to store a collection of unique elements. It is
available in the java.util package and is commonly used when duplicate
values are not allowed.
Unlike the List interface, a Set does not allow duplicate elements.
Additionally, most Set implementations do not guarantee the order of
elements, although some specialized implementations can maintain order or
provide sorted data.
The Set interface is ideal for scenarios where you want to ensure that
every element in the collection is unique.
Key Characteristics of the Set Interface
The Set interface provides several important features that distinguish it
from other collection types.
Unique Elements Only
A Set automatically prevents duplicate entries. If an attempt is made to
add a duplicate element, it will simply be ignored.
No Index-Based Access
Unlike a List, elements in a Set cannot be accessed using an index
position.
Unordered Storage
Most Set implementations do not maintain insertion order, though some
specific implementations provide ordering capabilities.
Common Implementations of Set
Java provides several classes that implement the Set interface, each with
different characteristics.
Table
Implementation
HashSet
Description
Stores unique elements with no guaranteed order
TreeSet Maintains elements in sorted order
LinkedHashSet Preserves insertion order
Tip:
Use a Set when your application requires unique values only, such as
tracking unique user IDs or filtering duplicate data.
Creating and Using a Set in Java
To work with a Set, you typically use one of its implementations such as
HashSet.
Example: Storing Unique Usernames
import java.util.HashSet;
import java.util.Set;
public class UniqueUserExample {
public static void main(String[] args) {
Set<String> usernames = new
HashSet<>();
usernames.add("alex01");
usernames.add("sophia22");
usernames.add("michael77");
usernames.add("alex01"); // Duplicate
value
System.out.println("Registered Users: " +
usernames);
}
}
In this example, "alex01" is added twice, but it appears only once
because Sets do not allow duplicate values.
Common Set Methods
The Set interface provides several useful methods for managing
elements.
Table
Method Description
add() Adds an element to the set if it does not already
exist
remove() Removes a specified element from the set
contains() Checks whether a specific element exists in the
set
size() Returns the total number of elements
clear() Removes all elements from the set
Example: Using Set Methods
import java.util.HashSet;
import java.util.Set;
public class CourseSetExample {
public static void main(String[] args) {
Set<String> courses = new
HashSet<>();
courses.add("Artificial
Intelligence");
courses.add("Machine
Learning");
courses.add("Data Science");
System.out.println("Courses Available: " +
courses);
// Checking if an element
exists
if(courses.contains("Machine Learning"))
{
System.out.println("Machine
Learning course is available.");
}
// Removing an element
courses.remove("Data Science");
System.out.println("Updated Course List: "
+ courses);
System.out.println("Total Courses: " +
courses.size());
}
}
This example demonstrates how Sets can be used to store and manage
unique course names.