📜  查找两个集合之间的交集的Java程序

📅  最后修改于: 2022-05-13 01:55:10.819000             🧑  作者: Mango

查找两个集合之间的交集的Java程序

集合意味着一组不同的类和接口被组合成一个具有相似功能的单元称为集合和框架,我们非常了解它提供了一个 在Java表示和操作集合的预定义架构。在这里,我们将讨论如何找到两个集合之间的交集,即如果第一个集合在第二个集合中不可用,则从第一个集合中删除所有元素。

在本文中,我们将使用两个集合框架类 vector 类和 ArrayList 类来查找两个集合之间的交集。

方法:

  1. 使用 ArrayList.contains() 方法
  2. 使用 Vector.retainAll() 方法

方法一:

  • 将元素存储在第一个集合和第二个集合(数组列表)中。
  • 现在迭代第一个集合并检查第二个集合是否包含第一个集合的元素。
  • 如果不包含从第一个集合中删除元素。
  • 打印第一个集合。

例子:



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
    RemoveElements(ArrayList A,
                   ArrayList 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 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 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 B = new ArrayList<>();
  
        // Inserting elements to ArrayList object
        // Custom input entries
        B.add(1);
        B.add(2);
        B.add(3);
  
        // Calling the Function
        ArrayList UpdatedCollection
            = RemoveElements(A, B);
  
        // Lastly printing the updated collection
        print(A);
    }
}


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 RemoveElements(Vector A,
                                          Vector B)
    {
        A.retainAll(B);
        // Returning the update ArrayList
        return A;
    }
    // Method 2 of this class
    // To print the collection
    static void print(Vector 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 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 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 UpdatedCollection
            = RemoveElements(A, B);
  
        // Printing the updated collection
        print(A);
    }
}


输出
1 2 3 

方法二:使用 Vector.retainAll() 方法

  • 将元素存储在第一个集合和第二个集合 (Vector) 中。
  • 现在使用Vector.retainAll() 方法
  • 打印第一个集合。

例子:

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 RemoveElements(Vector A,
                                          Vector B)
    {
        A.retainAll(B);
        // Returning the update ArrayList
        return A;
    }
    // Method 2 of this class
    // To print the collection
    static void print(Vector 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 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 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 UpdatedCollection
            = RemoveElements(A, B);
  
        // Printing the updated collection
        print(A);
    }
}
输出
1 2 3