forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.java
More file actions
38 lines (31 loc) · 926 Bytes
/
Copy pathExample4.java
File metadata and controls
38 lines (31 loc) · 926 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
package chapter_14;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
/**
* 熟悉标准库
*
* @author biezhi
* @date 2018/9/24
*/
public class Example4 {
private List<Integer> list = Arrays.asList(2, 3, 3, 3);
// public List<Integer> unique(List<Integer> elements) {
// List<Integer> result = new ArrayList<>();
// for (Integer element : elements) {
// if (!result.contains(element)) {
// result.add(element);
// }
// }
// return result;
// }
// public List<Integer> unique(List<Integer> elements) {
// return new ArrayList<>(new HashSet<>(elements));
// }
public List<Integer> unique(List<Integer> elements) {
return elements.stream().distinct().collect(toList());
}
}