Collections Framework - The Set Interface

In this guide, we will learn about Set interface, Set interface methods and Set interface implementation classes.

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. 

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Set Interface Hierarchy Diagram




Set Interface Class Diagram



Set Interface APIs/Methods

Below are the important Set interface methods with description:

  • boolean add(E e) - This method adds the specified element to this set if it is not already present (optional operation).
  • boolean addAll(Collection<? extends E> c) - This method adds all of the elements in the specified collection to this set if they're not already present (optional operation).
  • _void clear() _- This method removes all of the elements from this set (optional operation).
  • boolean contains(Object o) - This method returns true if this set contains the specified element.
  • boolean containsAll(Collection<?> c) - This method returns true if this set contains all of the elements of the specified collection.
  • boolean equals(Object o) - This method compares the specified object with this set for equality.
  • int hashCode() - This method returns the hash code value for this set.
  • boolean isEmpty() - This method returns true if this set contains no elements.
  • Iterator iterator() - This method returns an iterator over the elements in this set.
  • boolean remove(Object o) - This method removes the specified element from this set if it is present (optional operation).
  • boolean removeAll(Collection<?> c) - This method removes from this set all of its elements that are contained in the specified collection (optional operation).
  • boolean retainAll(Collection<?> c) - This method retains only the elements in this set that are contained in the specified collection (optional operation).
  • int size() - This method returns the number of elements in this set (its cardinality).
  • default Spliterator spliterator() - This method creates a Spliterator over the elements in this set.
  • _Object[] toArray() _- This method returns an array containing all of the elements in this set.
  • T[] toArray(T[] a) - This method returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Set Interface with It's HashSet Implementation Class Example

Let's create a simple example Set interface using the HashSet implementation class:

import java.util.HashSet;

import java.util.Set;

public class CreateHashSetExample {
    public static void main(String[] args) {
        // Creating a HashSet
     Set<String> daysOfWeek = new HashSet<>();

        // Adding new elements to the HashSet
        daysOfWeek.add("Monday");
        daysOfWeek.add("Tuesday");
        daysOfWeek.add("Wednesday");
        daysOfWeek.add("Thursday");
        daysOfWeek.add("Friday");
        daysOfWeek.add("Saturday");
        daysOfWeek.add("Sunday");

        // Adding duplicate elements will be ignored
        daysOfWeek.add("Monday");
        System.out.println(daysOfWeek);
    }
}
Output:
[Monday, Thursday, Friday, Sunday, Wednesday, Tuesday, Saturday]

Set Interface with It's LinkedHashSet Implementation Class Example

// Creating a HashSet
LinkedHashSet<String> daysOfWeek = new LinkedHashSet<>();

// Adding new elements to the HashSet
daysOfWeek.add("Monday");
daysOfWeek.add("Tuesday");
daysOfWeek.add("Wednesday");
daysOfWeek.add("Thursday");
daysOfWeek.add("Friday");
daysOfWeek.add("Saturday");
daysOfWeek.add("Sunday");

// Adding duplicate elements will be ignored
daysOfWeek.add("Monday");

System.out.println(daysOfWeek);
Output:
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Read more about LinkedHashSet class with examples at Guide to LinkedHashSet Class.

Set Interface with It's TreeSet Implementation Class Example

// Creating a TreeSet
TreeSet<String> fruits = new TreeSet<>();

// Adding new elements to a TreeSet
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Pineapple");
fruits.add("Orange");

System.out.println("Fruits Set : " + fruits);

// Duplicate elements are ignored
fruits.add("Apple");
System.out.println("After adding duplicate element \"Apple\" : " + fruits);

// This will be allowed because it's in lowercase.
fruits.add("banana");
System.out.println("After adding \"banana\" : " + fruits);
Output:
Fruits Set : [Apple, Banana, Orange, Pineapple]
After adding duplicate element "Apple" : [Apple, Banana, Orange, Pineapple]
After adding "banana" : [Apple, Banana, Orange, Pineapple, banana]
Note that in this example, duplicate elements are ignored.

Set Interface Implementation Classes

The Java platform contains three general-purpose Set implementations: HashSetTreeSet, and LinkedHashSet.
  • HashSet, which stores its elements in a hash table, is the best-performing implementation; however, it makes no guarantees concerning the order of iteration.
  • TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.
  • LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments