Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Find the Intersection Between Two Collection

  • Last Updated : 28 Apr, 2021
Geek Week

Collection means a set of different classes and interfaces are group together into a single unit that has similar functions are called collection and framework we know very that provides a predefined architecture to represents and manipulate collections in java.  Here we will be discussing discuss out how to find the intersection between the two collections i.e Remove all the elements from the first collection if it is not available in the second collection.

In this article we will use two collection framework classes vector class and ArrayList class to find the intersection between the two collection.

Methods:

  1. Using ArrayList.contains() Method
  2. Using Vector.retainAll() method

Approach 1: 

  • Store the elements in the First collection and in the second collection (Array List).
  • Now Iterate the First collection and checks whether the second collection contains elements of the first collection or not.
  • If not contains remove the element from the first collection.
  • Print the first collection.

Example:



Java




// Java Program to Remove All the Elements from the First
// Collection if it is not Available in the Second
// Using ArrayList.contains() Method  
  
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Method 1 of this class
    // To remove the elements from the collection
    static ArrayList<Integer>
    RemoveElements(ArrayList<Integer> A,
                   ArrayList<Integer> B)
    {
        // Iterating over elements in object
        // using for loop
        for (int i = 0; i < A.size(); ++i) {
  
            // Removing the elements from the collection
            if (B.contains(A.get(i)) == false) {
                A.remove(i);
            }
        }
  
        // Returning the update ArrayList
        return A;
    }
  
    // Method 2 of this class
    // To print the collection
    static void print(ArrayList<Integer> A)
    {
        // Iterating over elements in object
        // using for-each loop
        for (int element : A) {
  
            // Printing the elements of the linked list
            System.out.print(element + " ");
        }
    }
  
    // Method 3 of this class
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of ArrayList class
        // Declaring object of Integer type
        ArrayList<Integer> A = new ArrayList<>();
  
        // Inserting elements to ArrayList object
        // Custom input entries
        A.add(1);
        A.add(2);
        A.add(3);
        A.add(4);
  
        // Creating another object of ArrayList class
        // Again declaring object of Integer type
        ArrayList<Integer> B = new ArrayList<>();
  
        // Inserting elements to ArrayList object
        // Custom input entries
        B.add(1);
        B.add(2);
        B.add(3);
  
        // Calling the Function
        ArrayList<Integer> UpdatedCollection
            = RemoveElements(A, B);
  
        // Lastly printing the updated collection
        print(A);
    }
}
Output
1 2 3 

Approach 2: Using Vector.retainAll() method

  • Store the elements in the First collection and in the second collection (Vector).
  • Now use Vector.retainAll() method
  • Print the first collection.

Example:

Java




// Java Program to Remove All the Elements from the First
// Collection if it is not Available in the Second
// Using Vector.retainAll() method
  
// Importing libraries
import java.io.*;
import java.util.*;
  
// Main class
public class GFG {
    // Method 1 of this class
    // To remove the elements from the collection
    static Vector<Integer> RemoveElements(Vector<Integer> A,
                                          Vector<Integer> B)
    {
        A.retainAll(B);
        // Returning the update ArrayList
        return A;
    }
    // Method 2 of this class
    // To print the collection
    static void print(Vector<Integer> A)
    {
        // Iterating elements in object using for loop
        for (int element : A) {
  
            // Printing the elements of the linked list
            System.out.print(element + " ");
        }
    }
  
    // Method 3 of this class
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an ArrayList object
        // Declaring object of integer type
        Vector<Integer> A = new Vector<>();
  
        // Inserting elements in the ArrayList
        // Custom input entries
        A.add(1);
        A.add(2);
        A.add(3);
        A.add(4);
  
        // Creating another ArrayList
        // Again declaring object of integer type
        Vector<Integer> B = new Vector<>();
  
        // Inserting elements in the ArrayList
        // Custom input entries
        B.add(1);
        B.add(2);
        B.add(3);
  
        // Calling the Method1 now to
        // remove the elements from the collection
        Vector<Integer> UpdatedCollection
            = RemoveElements(A, B);
  
        // Printing the updated collection
        print(A);
    }
}
Output
1 2 3 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :