Java Set Interface

In Java Set interface extends the Collection interface and provides the skeletal for the classes that implement it. The Set in Java ensures the uniqueness of its element but it is an unordered collection. There are some classes that implement the Set interface and holds unique elements only. The Set interface in java inherits the methods from Collection interface also. The Set interface is implemented by various classes like HashSet, EnumSet, LinkedHashSet, and TreeSet. We will discuss each class in a separate post.

Java Set interface

Properties of Java Set interface

Set interface in java has some properties that extend by many classes and used to handle the collections of objects.

1. Whoever class extends the Set interface can’t contain duplicate elements. It means a collection of the set can contain only unique values.

2. A set is unordered collection of objects. We can’t find the order of element.

3. Since Java 8Set interface has some default methods, those are automatically inherited in derived classes

4. It supports generic concepts, we can make a set as class specific.

Important Methods

Let’s discuss the methods of the Set interface. There are a number of set methods but some methods inherited from Collection interface and some are it’s own. Let’s discuss the most common and useful set methods.

1. int size(): This method returns an int value. It returns the size of the set and tells how many objects presented in Set.
2. boolean isEmpty(): This method returns a boolean value and we can find whether the Set is empty or not.
3. boolean contains(Object o): This method returns a boolean value and uses to find whether the given Object o exists or not.
4. boolean containsAll(Collection c): This method returns a boolean value and uses to find whether the given Collection c exists or not.
5. Iterator iterator(): This method returns an object of the Iterator. We can iterate over the objects by use of iterator.
6. Object[] toArray(): This method returns an Array of Objects. The array contains all the objects of Set.
7. boolean add(E e): This method is used to add the given object in Set. If an object already exists it doesn’t add it.
8. boolean addAll(Collection c): This method adds all the objects from given collection c to Set.
9. boolean remove(Object o): The method is used to remove the given object from Set.
10. boolean retainAll(Collection c): This method is used to retains the elements from the Set only those are common in given Collection c and Set.
11. boolean removeAll(Collection c): This method is used to remove all objects from the Set that are common in Set and collection c. It returns a boolean value.
12. void clear(): To remove all the elements from the Set.

Java set interface implementation classes in java

The Set interface is implemented by HashSet, EnumSet, LinkedHashSet, and TreeSet. We will discuss each implementation in a separate post.

Let’s see how HashSet implements the Set interface and use it.

import java.util.HashSet;
import java.util.Set;

public class HashSetExample
    public static void main(String[] args) 
    	Set<Integer> integerHashSet = new HashSet<Integer>();
    	System.out.println("HashSet of Integer: "+ integerHashSet);
    	Set<String> stringHashSet = new HashSet<String>();
    	System.out.println("HashSet of String: "+ stringHashSet);

Output: HashSet of Integer: [1, 2, 3, 4, 5]
HashSet of String: [Learning, Hi, Java, Goal, Website]

Let’s see how LinkedHashSet implements the Set interface and use it.

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetExample
    public static void main(String[] args) 
    	Set<Integer> integerLinkedHashSet = new LinkedHashSet<Integer>();
    	System.out.println("LinkedHashSet of Integer: "+ integerLinkedHashSet);
    	Set<String> stringLinkedHashSet = new LinkedHashSet<String>();
    	System.out.println("LinkedHashSet of String: "+ stringLinkedHashSet);

Output: LinkedHashSet of Integer: [1, 2, 3, 4, 5]
LinkedHashSet of String: [Hi, Java, Goal, Learning, Website]

Let’s see how TreeSet implements the Set interface and use it.

import java.util.Set;
import java.util.TreeSet;

public class TreeSetExample
    public static void main(String[] args) 
    	Set<Integer> integerTreeSet = new TreeSet<Integer>();
    	System.out.println("TreeSet of Integer: "+ integerTreeSet);
    	Set<String> stringTreeSet = new TreeSet<String>();
    	System.out.println("TreeSet of String: "+ stringTreeSet);

Output: TreeSet of Integer: [1, 2, 3, 4, 5]
TreeSet of String: [Goal, Hi, Java, Learning, Website]

Leave a Comment