如何比较 Java 中的两个集合?

原文:https://www . geesforgeks . org/如何比较两个 java 集合/

Java 集合提供了一个存储和操作对象组的架构。这里我们将看到如何在 Java 中比较集合中的元素。

步骤:

  • Get two inputs with the function of aslist () .
  • Use the collections.sort () method to sort them.
  • Use the equals () function for comparison.
  • Print out. (True means they are equal, false means they are different)

例 1:

Java

// Java program implementing
// Comparing elements of Collections

import java.util.*;
import java.io.*;

public class ArrayCompareExample {

    // main function accepting string arguments
    public static void main(String[] args)
    {
        // create listA
        ArrayList<String> listA
            = new ArrayList<>(Arrays.asList("a", "b", "c"));

        // create listB
        ArrayList<String> listB
            = new ArrayList<>(Arrays.asList("a", "b", "d"));

        // sorting both lists
        Collections.sort(listA);
        Collections.sort(listB);

        // Compare lists using
        // equals() method
        boolean isEqual = listA.equals(listB);

        // print output on screen (true or false)
        System.out.println(isEqual);
    }
}

输出

false

例 2:

Java

// Java program implementing
// Comparing elements of Collections

import java.util.*;
import java.io.*;

public class ArrayCompareExample {

    // main function accepting string arguments
    public static void main(String[] args)
    {
        // create listA
        ArrayList<Integer> listA
            = new ArrayList<>(Arrays.asList(3, 4, 5));

        // create listB
        ArrayList<Integer> listB
            = new ArrayList<>(Arrays.asList(4, 5, 3));

        // sorting both lists
        Collections.sort(listA);
        Collections.sort(listB);

        // Compare lists using
        // equals() method
        boolean isEqual = listA.equals(listB);

        // print output on screen (true or false)
        System.out.println(isEqual);
    }
}

输出

true