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

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.

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.

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.

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.