Java Program to Find the Intersection Between Two Collection Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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: Using ArrayList.contains() MethodUsing 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); } } Output1 2 3 Time Complexity: O(n) Space Complexity: O(n) Approach 2: Using Vector.retainAll() method Store the elements in the First collection and in the second collection (Vector).Now use Vector.retainAll() methodPrint 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); } } Output1 2 3 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Find the Intersection Between Two Collection zack_aayush Follow Improve Article Tags : Java Java Programs Java-Collections Practice Tags : JavaJava-Collections Similar Reads How to Find the Intersection of Two Strings in Java? We know that the Strings are a collection of characters so if we want to find the intersection of two Strings the simple way is we can compare each character of the first String with the characters of the Second String. Example to Find the Intersection of Two Strings in JavaThere are two strings ava 2 min read Find the Intersection of Two HashSets in Java HashSets is a type of Collection in Java that cannot contain any duplicate values. It is often used when we need to maintain a unique data set in our application, it uses hashing internally to store the elements in it, so operations like searching, insertion, and deletion take only a constant amount 6 min read Java Program to Find Common Elements Between Two Arrays Given two arrays and our task is to find their common elements. Examples:Input: Array1 = ["Article", "for", "Geeks", "for", "Geeks"], Array2 = ["Article", "Geeks", "Geeks"]Output: [Article, Geeks]Input: Array1 = ["a", "b", "c", "d", "e", "f"], Array2 = ["b", "d", "e", "h", "g", "c"]Output: [b, c, d, 4 min read Java Program To Remove All The Duplicate Entries From The Collection As we know that the HashSet contains only unique elements, ie no duplicate entries are allowed, and since our aim is to remove the duplicate entries from the collection, so for removing all the duplicate entries from the collection, we will use HashSet.The HashSet class implements the Set interface, 3 min read How to Find the Intersection and Union of Two PriorityQueues in Java? In Java, PriorityQueue is an implementation of the Queue Interface. It can be used to provide a way to store the elements in a queue based on their priority basis. This means high-priority elements can store first compared to lower-priority elements in the queue. In this article, we will be learning 2 min read Java Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2- 5 min read Java Program to Add the Data from the Specified Collection in the Current Collection The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a 3 min read Java Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read Java Program For Union And Intersection Of Two Linked Lists Write a java program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:In 10 min read Using Above Below Primitive to Test Whether Two Lines Intersect in Java Above Below Primitive is the method to check whether the lines are intersecting or not by comparing their endpoints. Here is a JAVA program which uses above below primitive to test whether two lines intersect with each other or not. This approach only returns true if one endpoint of the line is on t 3 min read Like