Set Intersection in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The set, as the name suggests, is an interface that resides in java.util package. A set is generally termed as a collection interface that is used for storing an unordered list and restricts the storage of duplicate entities in the set.

Introduction

A set cares about uniqueness and it doesn’t allow duplicates. The interface of the set has the following properties:

  • The elements inside the set are not null
  • No two elements can be equal in set
  • The insertion order is not preserved in the set

The hierarchy of the set is as follows: The set interface is divided into two types. The first type contains an unordered set which is a Hash set and a Linked Hash set while the second type contains an ordered set like a sorted set, a navigable set, and a tree set.

HashSet:

  • A HashSet is an unordered, unsorted set.
  • It uses the hashcode of the object being inserted, so the more efficient your hashCode() implementation the better access performance you will get.
  • Use this class when you want a collection with no duplicates and you don’t care about order when you iterate through it.

LinkedHashSet:

  • A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked list across all elements.
  • Use this class instead of HashSet when you care about the iteration order.
  • When you iterate through a HashSet the order is unpredictable, while LinkedHashSet lets you iterate through the elements in the order in which they were inserted.

TreeSet:

  • The TreeSet guarantees that the elements will be in ascending order, according to the natural order.
  • Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be rather than relying on the ordering defined by the element’s class by using a comparable or comparator.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Java Program to Find a Set Intersection (BruteForce)

Below is the code that demonstrates the set insertion and finding the insertion between the two sets in Java.

Output:

Explanation:

  • This Java code demonstrates how to find the intersection of two sets using HashSet. It creates two sets, one using add method and another using constructor.
  • It then prints the common elements between them using nested loops and if condition.
  • The output displays the common elements of both sets.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Java Program to Find a Set Intersection Using retainAll() Method

  • The retainAll() is a method of the Set interface that is used to keep only the elements that are common between two sets.
  • The retainAll() method returns true if the set was modified as a result of the operation, and false otherwise.
  • The retainAll() method can be used with any class that implements the Set interface, such as HashSet, TreeSet, and LinkedHashSet.

Let's see an example of retainAll() method.

Output:

Explanation:

  • In this example, set1 has elements Hello, World and Code while set2 has elements Tue Sun Mon, and Code.
  • Among both sets, the value Code is the same in both sets and hence is added in the intersection set.
  • As set1 is changed due to the retainAll() method operation, true is returned.

Java Program to Find a Set Intersection Using Guava Library

Let's see a Java program to find an intersection of two sets using the Guava library.

Output:

Explanation:

  • In this example, the Sets.intersection() method is used for finding the intersection between the two set.
  • This is known as the Guava method and is used for finding the intersection between the two sets.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • The set, as the name suggests, is an interface that resides in java.util package. A set is generally termed as a collection interface that is used for storing an unordered list and restricts the storage of duplicate entities in the set.
  • A set cares about uniqueness and it doesn’t allow duplicates. The interface of the set has the following properties:
    • The elements inside the set are not null
    • No two elements can be equal in set
    • The insertion order is not preserved in the set
  • There are three ways for finding the intersection of two sets: brute force, using the retainAll() method, and using the Guava library.
  • The retainAll() method keeps only the elements that are common between two sets and returns true if the set was modified as a result of the operation.
  • Finally, the Guava library is introduced as an alternative to the brute force method, which can handle larger sets with better performance.