Skip to content

Instantly share code, notes, and snippets.

@svenakela
Last active May 2, 2019 12:58
Show Gist options
  • Save svenakela/749771ee67221dc557a88d9a0575c16c to your computer and use it in GitHub Desktop.
Save svenakela/749771ee67221dc557a88d9a0575c16c to your computer and use it in GitHub Desktop.
Finding duplicates in a list
package barky;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CollectionDuplicateTest {
@Test
void findDuplicatesOldSchool() {
final List<Integer> integers = List.of(1, 2, 3, 3, 3, 2, 4, 5, 6, 2, 5, 7, 8, 9);
final Set<Integer> newValue = new HashSet<>();
final Set<Integer> collector = new HashSet<>();
for(final Integer integer: integers) {
if (!newValue.add(integer)) {
collector.add(integer);
}
}
Assertions.assertArrayEquals(collector.toArray(), List.of(2, 3, 5).toArray());
}
@Test
void findDuplicatesStreamy() {
final List<Integer> integers = List.of(1, 2, 3, 3, 3, 2, 4, 5, 6, 2, 5, 7, 8, 9);
final Set<Integer> newValue = new HashSet<>();
final Set<Integer> collector =
integers.stream()
.filter(v -> !newValue.add(v))
.collect(Collectors.toSet());
Assertions.assertArrayEquals(collector.toArray(), List.of(2, 3, 5).toArray());
}
@Test
void findDuplicatesStreamStreamyToo() {
final List<Integer> integers = List.of(1, 2, 3, 3, 3, 2, 4, 5, 6, 2, 5, 7, 8, 9);
final Set<Integer> collector =
integers.stream()
.filter(v -> Collections.frequency(integers, v) > 1)
.collect(Collectors.toSet());
Assertions.assertArrayEquals(collector.toArray(), List.of(2, 3, 5).toArray());
}
/**
* Not my idea, added because it is nice and totally immutable
* in the stream and is not working with any external objects.
* https://stackoverflow.com/a/52296246/460544
*/
@Test
void findDuplicatesStreamOnly() {
final List<Integer> integers = List.of(1, 2, 3, 3, 3, 2, 4, 5, 6, 2, 5, 7, 8, 9);
final Set<Integer> collector =
integers.stream()
.collect(Collectors.groupingBy(Function.identity()))
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
Assertions.assertArrayEquals(collector.toArray(), List.of(2, 3, 5).toArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment