Set Interface

 The Set interface of the Java Collections framework provides the features of the mathematical set in Java. It extends the Collection interface.

Unlike the List interface, sets cannot contain duplicate elements.

Classes that implement Set:

  • LinkedHashSet
  • EnumSet

How to use Set?

In Java, we must import java.util.Set package in order to use Set.

Set<String> animals = new HashSet<>();

Methods of Set:

  • add() - adds the specified element to the set
  • addAll() - adds all the elements of the specified collection to the set
  • iterator() - returns an iterator that can be used to access elements of the set sequentially
  • remove() - removes the specified element from the set
  • removeAll() - removes all the elements from the set that is present in another specified set
  • retainAll() - retains all the elements in the set that are also present in another specified set
  • clear() - removes all the elements from the set
  • size() - returns the length (number of elements) of the set
  • toArray() - returns an array containing all the elements of the set
  • contains() - returns true if the set contains the specified element
  • containsAll() - returns true if the set contains all the elements of the specified collection
  • hashCode() - returns a hash code value (address of the element in the set)
Implementation of the Set Interface

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

class Main {

    public static void main(String[] args) {
        // Creating a set using the HashSet class
        Set<Integer> set1 = new HashSet<>();

        // Add elements to the set1
        set1.add(2);
        set1.add(3);
        System.out.println("Set1: " + set1);

        // Creating another set using the HashSet class
        Set<Integer> set2 = new HashSet<>();

        // Add elements
        set2.add(1);
        set2.add(2);
        System.out.println("Set2: " + set2);

        // Union of two sets
        set2.addAll(set1);
        System.out.println("Union is: " + set2);
    }
}

Output:

Set1: [2, 3]
Set2: [1, 2]
Union is: [1, 2, 3]

Post a Comment