-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubLists.java
More file actions
32 lines (26 loc) Β· 1.07 KB
/
Copy pathSubLists.java
File metadata and controls
32 lines (26 loc) Β· 1.07 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
package Chapter7.Day47;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class SubLists {
public static <E> Stream<List<E>> of(List<E> list) {
return Stream.concat(Stream.of(Collections.emptyList()), // EMPTY_LISTλ‘ νλ©΄ unchecked, νλ³νκΉμ§ ν΄μ£Όλ emptyList() μ°μ
prefixes(list)
.flatMap(SubLists::suffixes));
}
// (a, b, c)
public static <E> Stream<List<E>> prefixes(List<E> list) {
return IntStream.rangeClosed(1, list.size()) // list.size() ν¬ν¨
.mapToObj(end -> list.subList(0, end)); // (a) (a, b) (a, b, c)
}
// (a, b, c)
public static <E> Stream<List<E>> suffixes(List<E> list) {
return IntStream.rangeClosed(0, list.size()) // list.size() ν¬ν¨
.mapToObj(start -> list.subList(start, list.size())); // (a,b,c) (b,c) (c)
}
public static void main(String[] args) {
Stream<List<String>> of = of(List.of("a", "b", "c"));
of.forEach(System.out::println);
}
}