Java Program to Calculate the Intersection of two Sets

   
Java Program to Calculate the intersection of two sets

What is a Set in Java ?

In Java, a Set is a collection that cannot contain duplicate elements. It is an interface in the Java Collection Framework, and has several implementing classes such as HashSet, TreeSet, and LinkedHashSet.

A Set can be useful when you need to store a collection of unique items and perform operations such as testing for membership, adding or removing elements, or iterating through the elements.

What is Set Intersection ?

In set theory, the intersection of two sets is a new set that contains only the elements that are present in both of the original sets. It is denoted by the symbol .

In Java, the retainAll() method of the Set interface can be used to perform the intersection of two sets. The retainAll() method modifies the current set so that it only contains elements that are also contained in a specified collection.

Example 1 : Calculation of  Intersection  using retainAll() Method :

Run

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

public class Main {
    public static void main(String[] args) {
        Set <String> setA = new HashSet<>();
        setA.add("apple");
        setA.add("banana");
        setA.add("orange");

        Set <String> setB = new HashSet<>();
        setB.add("banana");
        setB.add("kiwi");
        setB.add("orange");

        setA.retainAll(setB);
        System.out.println("Intersection of setA and setB: " + setA);
    }
}

Output :

Intersection of setA and setB: [banana, orange]

Example 2 : Calculation of  Intersection  using retainAll() Method :

Run

import java.util.HashSet;
import java.util.Set;
public class Main {
    
    public static void main(String[] args) {
        //Declaration of set1
        Set <String> set1 = new HashSet();

        //Adding elements to set1
        set1.add("Red");
        set1.add("Green");
        set1.add("Black");
        set1.add("White");
        set1.add("Pink");

        System.out.println("Set1: " + set1);
        
        //Declaration of set2
        Set <String> set2 = new HashSet();

        //Adding elements in set2
        set2.add("Red");
        set2.add("Green");
        set2.add("Black");
        set2.add("Pink");
        System.out.println("Set2: " + set2);
        set1.retainAll(set2);
        System.out.println("Intersection of two sets: " + set1);
    }
}

Output :

Set1: [Red, White, Pink, Black, Green]
Set2: [Red, Pink, Black, Green]
Intersection of two sets: [Red, Pink, Black, Green]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription