-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinct.java
More file actions
34 lines (28 loc) · 1.31 KB
/
Copy pathDistinct.java
File metadata and controls
34 lines (28 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
public class Distinct {
private static void testDistinct() {
// distinct возвращает stream без дубликатов, при этом для упорядоченного стрима
// (например, коллекция на основе list) порядок стабилен, для неупорядоченного — порядок не гарантируется.
Collection<String> ordered = Arrays.asList("a3", "a2", "a2", "a3", "a1", "a2", "a2");
Collection<String> nonOrdered = new HashSet<>(ordered);
System.out.println("nonOrdered = " + nonOrdered);
// Получение коллекции без дубликатов
List<String> distinct = nonOrdered
.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("distinct = " + distinct);
List<String> distinctOrdered = ordered
.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("distinctOrdered = " + distinctOrdered);
}
public static void main(String[] args) {
testDistinct();
}
}