Java Set Interface
The Java Set Interface
is part of the Java Collection Framework. It extends the Collection
interface and is an unordered collection of objects, in which duplicate values cannot be stored. It is an interface that implements a mathematical set. This interface contains the methods inherited from the Collection
interface and adds a feature that restricts the insertion of duplicate elements.
Let's dive into the specifics of the Set
interface and how to use it effectively in your Java programs.
Declaring a Set
In Java, you can declare a Set like this:
Set<data-type> set = new HashSet<data-type>();
Here data-type
can be any Java data type, and HashSet
is a class that implements Set
. There are multiple classes in Java that implements Set
interface, and we will look into those later.
Set Interface Methods
The Set
interface includes all the methods of the Collection
interface. Some of the commonly used methods are:
- add(element): This method is used to add elements to the set. The function returns
true
if the element is not already present in the set, otherwisefalse
. - addAll(collection): This method is used to add all the elements from the specified collection to the set.
- remove(element): This method is used to remove an element from the set.
- removeAll(collection): This method is used to remove all the elements from the set which are present in the specified collection.
- contains(element): This method is used to check whether an element is present in the set or not.
- size(): This method is used to get the size of the set.
- isEmpty(): This method is used to check if the set is empty.
Implementations of Set Interface
There are three classes that implement the Set
interface in Java:
HashSet: This class implements the
Set
interface, backed by a hash table (actually aHashMap
instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.LinkedHashSet: This class is an ordered version of
HashSet
that maintains a doubly-linked List across all elements.TreeSet: This class implements the
Set
interface that uses a tree for storage. Objects are stored in a sorted and ascending order.
Set Interface Example
Let's look at a simple example demonstrating the use of Set
:
import java.util.*;
class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("Hello");
set.add("World");
set.add("!");
System.out.println("Set: " + set);
System.out.println("Size of Set: " + set.size());
if(set.contains("Hello")) {
System.out.println("Hello is in the set.");
}
set.remove("Hello");
System.out.println("After removing 'Hello': " + set);
}
}
In this tutorial, we have seen the basics of the Set
interface in Java, its methods and its different implementations. It is a powerful interface that allows you to manipulate collections of unique elements. Make use of it in your programs whenever you need a collection that cannot contain duplicate elements. Happy Coding!