SoFunction
Updated on 2025-04-12

Summary of the method of quickly implementing repeated judgments in Java collection List

In Java writing code, you often encounter some duplicate judgments or deduplication operations, including:

  • Data cleaning: Data obtained from a database or other source may contain duplicates and require deduplication.
  • User input: The data passed in the previous section may have duplication, and the duplication needs to be checked and removed to avoid errors or inconsistencies during processing.
  • List merge: After merging multiple lists, duplicate elements may be generated, and deduplication is also a common operation after merging.

The following is used to record the deduplication operation you will use to prevent forgetting:

1. Use Streaming API

Use the Streaming API to elegantly check duplicate data through the Streaming API (recommended)

import ;
import ;
import ;
import ;

public class CheckDuplicate {
    public static void main(String[] args) {
        List<String> list = ("A", "B", "C", "A");

        // Find duplicate elements        Set<String> duplicates = ()
                .filter(item -> ().filter(item::equals).count() > 1)
                .collect(());

        ("Repeat elements: " + duplicates);
        ("Whether duplicate data exists: " + !());
    }
}

streamIt is a very powerful way, which can conveniently use declarative methods to process the collection without explicitly manipulating elements in the collection. It provides a more functional programming model, allowing you to perform filtering, conversion, collection and other operations, and at the same time chain calls, improving the readability and simplicity of the code.

In comparisonforEachIs a method of the collection class that performs an operation on each element in the collection, usually a traversal operation. There is no streaming conversion or filtering function, but more to access elements one by one and perform certain operations.forEachThe typical usage of this is to traverse the set, but it is not suitable for streaming processing, hereforEachLogic needs to be written explicitly internally to calculate the occurrence of elements.

2. Quick check using native Set

Use native Set to quickly check, you can directly use HashSet or LinkedHashSet to judge

import ;
import ;
import ;
import ;

public class CheckDuplicate {
    public static void main(String[] args) {
        List<String> list = ("A", "B", "C", "A");

        Set<String> seen = new HashSet<>();
        boolean hasDuplicate = ().anyMatch(item -> !(item));

        ("Whether duplicate data exists: " + hasDuplicate);
    }
}

!(item)Will return:

  • true--Repeat elements,
  • false--Non-repeat element, first added

anyMatchIt will match all stream elements. Once a duplicate element that meets the conditions appears, the stream will automatically end, which is only suitable for determining whether there will be duplicate elements.

3. Use new HashSet<>(list)

Use new HashSet<>(list), and finally determine whether the size between the two lists is repeated

import ;

import ;
import ;
import ;

public class CheckDuplicate {
    public static void main(String[] args) {
        List&lt;String&gt; list = ("A", "B", "C", "A");

        // Create a HashSet using Hutool's tool method        Set&lt;String&gt; set = new HashSet&lt;&gt;(list);
        boolean hasDuplicate = () != ();

        ("Whether duplicate data exists: " + hasDuplicate);
    }
}

Comparing the size of the two sets, you can determine whether there are duplicate elements, because the set under Set is automatically deduplicated, and if there is duplicate, the set size will be reduced.

4. Use the () method

If you have a hutool tool, you can use the() method

import ;

import ;
import ;
import ;

public class CheckDuplicate {
    public static void main(String[] args) {
        List&lt;String&gt; list = ("A", "B", "C", "A", "B");

        // Statistics the number of occurrences of each element        Map&lt;String, Integer&gt; countMap = (list);
        
        // Print duplicate elements        ((key, value) -&gt; {
            if (value &gt; 1) {
                ("Repeat elements: " + key + ", number of occurrences: " + value);
            }
        });
    }
}

The () method can not only count the repeated elements that appear, but also count the frequency of occurrence. The return result is as follows:

Repeat element: A, number of occurrences: 2
Repeat element: B, occurrences: 2

Correspondingly, there is()Remove heavy weights,()Convert Set<> collection deduplication, and()Many interested partners can study the method.

Conclusion

In general, Java streaming is a very good way, and there are manyfilteriteratorfilter(), map(), collect()Other operations are convenient for handling complex logical link problems

Of course, if there is only some simple internal processing, direct foreach is the simplest and most efficient way to avoid stream conversion.

This is the article about how to quickly implement repeated judgments in Java collection List. For more related Java repetitive judgment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!