Java Set Interface: Working with Unique Collections

Discover the Java Set interface, a collection that ensures no duplicate elements. Learn how it inherits methods from the Collection interface and enforces rules for equals and hashCode to allow meaningful comparisons across different Set implementations.



Java - Set Interface

A Set is a Collection that does not allow duplicate elements. It represents the mathematical concept of a set.

The Set interface inherits methods from the Collection interface and enforces the rule that duplicate elements are prohibited. Additionally, it enhances the contract for equals and hashCode operations, allowing meaningful comparisons between different implementation types of Set.

Set Interface Methods

The methods declared by the Set interface are summarized in the following table:

Sr.No. Method & Description
1 add() - Adds an object to the collection.
2 clear() - Removes all objects from the collection.
3 contains() - Returns true if a specified object is an element within the collection.
4 isEmpty() - Returns true if the collection has no elements.
5 iterator() - Returns an Iterator object for the collection, which can be used to retrieve objects.
6 remove() - Removes a specified object from the collection.
7 size() - Returns the number of elements in the collection.

Set Interface Examples

The Set interface has implementations in various classes, including HashSet, TreeSet, and LinkedHashSet. Below are examples demonstrating the functionality of the Set interface.

Example: Implement Set Using HashSet

The following example demonstrates the use of a HashSet:

// File Name: SetDemo.java

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

public class SetDemo {
public static void main(String args[]) {
    int count[] = {34, 22, 10, 60, 30, 22};
    Set set = new HashSet<>();
    try {
        for (int i = 0; i < 5; i++) {
            set.add(count[i]);
        }
        System.out.println(set);
    } catch (Exception e) {}
}
}
Output
[34, 22, 10, 60, 30]

Example: Implement Set Using TreeSet

The following example demonstrates the use of a TreeSet:

// File Name: SetDemo.java

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

public class SetDemo {
public static void main(String args[]) {
    int count[] = {34, 22, 10, 60, 30, 22};
    Set set = new HashSet<>();
    try {
        for (int i = 0; i < 5; i++) {
            set.add(count[i]);
        }
        System.out.println(set);
        
        TreeSet sortedSet = new TreeSet<>(set);
        System.out.println("The sorted list is:");
        System.out.println(sortedSet);
        
        System.out.println("The first element of the set is: " + (Integer)sortedSet.first());
        System.out.println("The last element of the set is: " + (Integer)sortedSet.last());
    } catch (Exception e) {}
}
}
Output
[34, 22, 10, 60, 30]
The sorted list is:
[10, 22, 30, 34, 60]
The first element of the set is: 10
The last element of the set is: 60

Example: Implement Set Using LinkedHashSet

The following example demonstrates the use of a LinkedHashSet:

// File Name: SetDemo.java

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

public class SetDemo {
public static void main(String args[]) {
    int count[] = {34, 22, 10, 60, 30, 22};
    Set set = new LinkedHashSet<>();
    try {
        for (int i = 0; i < 5; i++) {
            set.add(count[i]);
        }
        System.out.println(set);
    } catch (Exception e) {}
}
}
Output
[34, 22, 10, 60, 30]