forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueNames.java
More file actions
38 lines (23 loc) · 725 Bytes
/
UniqueNames.java
File metadata and controls
38 lines (23 loc) · 725 Bytes
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
35
36
37
38
void main() {
List<String> names = List.of("Martin", "Lucy", "Peter",
"Martin", "Robert", "Peter");
System.out.println(unique(names));
System.out.println(unique2(names));
System.out.println(unique3(names));
}
List<String> unique(List<String> names) {
List<String> uniqueNames = new ArrayList<>();
names.forEach(e -> {
if (!uniqueNames.contains(e)) {
uniqueNames.add(e);
}
});
return uniqueNames;
}
List<String> unique2(List<String> names) {
return names.stream().distinct().toList();
}
List<String> unique3(List<String> names) {
HashSet<String> uniqueNames = new HashSet<>(names);
return new ArrayList<String>(uniqueNames);
}