[Java 8] Duplicate deletion (& duplicate check) with Stream

About duplicate deletion of list using Stream # distinct write.

Duplicate deletion

Duplicate deletion can be done by distinct and then change to the list with collect.

Duplicate deletion


List<Integer> list = Arrays.asList(1, 1, 2, 3, 4, 5);
//[1, 2, 3, 4, 5]become
List<Integer> ans = list.stream().distinct().collect(Collectors.toList());

double check

The code (comment section) given by @swordone was smarter, so it is better to use this for duplicate checking.

double check


List<Integer> list = Arrays.asList(1, 1, 2, 3, 4, 5);
boolean ans = (list.size() == new HashSet<>(list).size());

Old version

Although it is inefficient, I will leave the old version that uses stream for the time being.

double check


List<Integer> list = Arrays.asList(1, 1, 2, 3, 4, 5);
boolean ans = (list.size() == list.stream().distinct().count());

Articles that I used as a reference

-Generate List using Stream \ # map \ () and Stream \ #collect \ ()

Recommended Posts

[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Element existence check with Stream
Java8 Stream duplicate check (write neatly using Collector)
Java8 list conversion with Stream map
[Kotlin] Delete files with duplicate contents [Java]
Check heap usage with Java Flight Recorder
[JAVA] Stream type
Try Java 8 Stream
Java version check
Java Stream API
Java check process
Studying Java 8 (Stream)
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
Make Java Stream line breaks nice with eclipse
Install java with Homebrew
List processing to understand with pictures --java8 stream / javaslang-
[Java] Stream Collectors notes
Check Java toString () implementation
Change seats with java
Install Java with Ansible
[Java] Stream API-Stream generation
[Java] Stream API / map
Comfortable download with JAVA
Check the behavior of Java Intrinsic Locks with bpftrace
Switch java with direnv
Java8 Stream API practice
Java8 Stream reduction operation
Download Java with Ansible
Let's scrape with Java! !!
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Check coverage with Codecov in Java + Gradle + Wercker configuration
Build Java with Wercker
Output FizzBuzz with stream
Java8 Stream Rough Summary
Endian conversion with JAVA
Only the top level Stream can be parallelized with Java Stream.
How to handle exceptions coolly with Java 8 Stream or Optional
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
List processing to understand with pictures --java8 stream / javaslang --bonus
[Java] Check the difference between orElse and orElseGet with IntStream
Convert 2D array to csv format with Java 8 Stream API
[Maven] Disable .Net-based Assembly Analyzer with OWASP Dependency Check [Java]
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Check compliance with object-oriented exercise
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
Check CSV value with RSpec
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java to play with Function
Try DB connection with Java