About duplicate deletion of list using Stream # distinct
write.
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());
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());
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());
-Generate List using Stream \ # map \ () and Stream \ #collect \ ()
Recommended Posts