Home > Java > javaTutorial > How Can I Efficiently Compare Two Lists in Java and Find Common and Unique Elements?

How Can I Efficiently Compare Two Lists in Java and Find Common and Unique Elements?

DDD
Release: 2024-11-28 14:17:11
Original
835 people have browsed it

How Can I Efficiently Compare Two Lists in Java and Find Common and Unique Elements?

Comparing Two Lists in Java

You have two sets of data that you want to compare to determine how many elements they share. Assuming these are not Java List objects, consider the following approach:

Using ArrayList

Utilizing ArrayList, you can employ the retainAll method. This method preserves only elements in the first list that are also present in the second list. For example:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class ListComparison {
    public static void main(String[] args) {
        Collection<String> listOne = new ArrayList<>(Arrays.asList("milan", "dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
        Collection<String> listTwo = new ArrayList<>(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

        listOne.retainAll(listTwo);
        System.out.println(listOne);
    }
}
Copy after login

This code will print a list of the common elements between listOne and listTwo.

Using HashSet

If you want to include the repeated values (i.e., elements that appear multiple times in either list), you can use a HashSet instead of ArrayList. HashSet does not allow duplicates, so after the comparison, you can use the removeAll method to remove common elements from the combined set of elements in both lists. This will give you a list of unique non-common elements:

import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;

public class ListComparison {
    public static void main(String[] args) {

        Collection<String> listOne = Arrays.asList("milan", "iga",
                "dingo", "iga",
                "elpha", "iga",
                "hafil", "iga",
                "meat", "iga",
                "neeta.peeta", "iga");

        Collection<String> listTwo = Arrays.asList("hafil",
                "iga",
                "binga",
                "mike",
                "dingo", "dingo", "dingo");

        Collection<String> similar = new HashSet<>(listOne);
        Collection<String> different = new HashSet<>();
        different.addAll(listOne);
        different.addAll(listTwo);

        similar.retainAll(listTwo);
        different.removeAll(similar);

        System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);
    }
}
Copy after login

This modified code will output the similar and different elements between the two lists.

The above is the detailed content of How Can I Efficiently Compare Two Lists in Java and Find Common and Unique Elements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template