Skip to content

Commit 3f85759

Browse files
committed
[add] add ToListCollector and CollectorHarness
1 parent 874c3c4 commit 3f85759

File tree

3 files changed

+112
-12
lines changed

3 files changed

+112
-12
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.brianway.learning.java8.streamapi;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* 比较收集器的性能
7+
*/
8+
public class CollectorHarness {
9+
10+
public static void main(String[] args) {
11+
System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs");
12+
System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs");
13+
}
14+
15+
private static long execute(Consumer<Integer> primePartitioner) {
16+
long fastest = Long.MAX_VALUE;
17+
for (int i = 0; i < 10; i++) {
18+
long start = System.nanoTime();
19+
primePartitioner.accept(1_000_000);
20+
long duration = (System.nanoTime() - start) / 1_000_000;
21+
if (duration < fastest) fastest = duration;
22+
System.out.println("done in " + duration);
23+
}
24+
return fastest;
25+
}
26+
}

java8/src/main/java/com/brianway/learning/java8/streamapi/PartitionPrimeNumbers.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
import java.util.stream.Stream;
2121

2222
/**
23-
* 分区:分类的特殊情况。分区函数返回一个布尔值
23+
* 得到 100 以内的质数和非质数
24+
*
25+
* - 分区:分类的特殊情况。分区函数返回一个布尔值
26+
* - Collector
2427
*/
2528
public class PartitionPrimeNumbers {
2629

2730
public static void main(String... args) {
2831
System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100));
2932
System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithCustomCollector(100));
33+
System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithInlineCollector(100));
3034

3135
}
3236

@@ -62,22 +66,29 @@ public static <A> List<A> takeWhile(List<A> list, Predicate<A> p) {
6266
return list;
6367
}
6468

69+
/**
70+
* Collector
71+
* 1.建立新的结果容器: supplier 方法
72+
* 2.将元素添到结果容器: accumulator 方法
73+
* 3.对容器应用最终转换: finisher 方法
74+
* 4.合并两个结果容器: combiner
75+
* 5.characteristics 方法
76+
*/
6577
public static class PrimeNumbersCollector
6678
implements Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> {
6779

6880
@Override
6981
public Supplier<Map<Boolean, List<Integer>>> supplier() {
7082
return () -> new HashMap<Boolean, List<Integer>>() {{
71-
put(true, new ArrayList<Integer>());
72-
put(false, new ArrayList<Integer>());
83+
put(true, new ArrayList<>());
84+
put(false, new ArrayList<>());
7385
}};
7486
}
7587

7688
@Override
7789
public BiConsumer<Map<Boolean, List<Integer>>, Integer> accumulator() {
7890
return (Map<Boolean, List<Integer>> acc, Integer candidate) -> {
79-
acc.get(isPrime(acc.get(true),
80-
candidate))
91+
acc.get(isPrime(acc.get(true), candidate))
8192
.add(candidate);
8293
};
8394
}
@@ -102,17 +113,17 @@ public Set<Characteristics> characteristics() {
102113
}
103114
}
104115

105-
public Map<Boolean, List<Integer>> partitionPrimesWithInlineCollector(int n) {
116+
public static Map<Boolean, List<Integer>> partitionPrimesWithInlineCollector(int n) {
106117
return Stream.iterate(2, i -> i + 1).limit(n)
107118
.collect(
108119
() -> new HashMap<Boolean, List<Integer>>() {{
109-
put(true, new ArrayList<Integer>());
110-
put(false, new ArrayList<Integer>());
120+
put(true, new ArrayList<>());
121+
put(false, new ArrayList<>());
111122
}},
112-
(acc, candidate) -> {
113-
acc.get(isPrime(acc.get(true), candidate))
114-
.add(candidate);
115-
},
123+
(acc, candidate) ->
124+
acc.get(isPrime(acc.get(true), candidate))
125+
.add(candidate)
126+
,
116127
(map1, map2) -> {
117128
map1.get(true).addAll(map2.get(true));
118129
map1.get(false).addAll(map2.get(false));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.brianway.learning.java8.streamapi;
2+
3+
import static com.brianway.learning.java8.streamapi.Dish.menu;
4+
import static java.util.stream.Collector.Characteristics.CONCURRENT;
5+
import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.EnumSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
import java.util.function.BiConsumer;
13+
import java.util.function.BinaryOperator;
14+
import java.util.function.Function;
15+
import java.util.function.Supplier;
16+
import java.util.stream.Collector;
17+
import java.util.stream.Collectors;
18+
19+
/**
20+
* 自定义 ToListCollector
21+
*/
22+
public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
23+
24+
@Override
25+
public Supplier<List<T>> supplier() {
26+
return () -> new ArrayList<T>();
27+
}
28+
29+
@Override
30+
public BiConsumer<List<T>, T> accumulator() {
31+
return (list, item) -> list.add(item);
32+
}
33+
34+
@Override
35+
public Function<List<T>, List<T>> finisher() {
36+
return i -> i;
37+
}
38+
39+
@Override
40+
public BinaryOperator<List<T>> combiner() {
41+
return (list1, list2) -> {
42+
list1.addAll(list2);
43+
return list1;
44+
};
45+
}
46+
47+
@Override
48+
public Set<Characteristics> characteristics() {
49+
return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH, CONCURRENT));
50+
}
51+
52+
public static void main(String[] args) {
53+
List<Dish> dishes = menu.stream().collect(Collectors.toList());
54+
System.out.println(dishes);
55+
dishes = menu.stream().collect(new ToListCollector<>());
56+
System.out.println(dishes);
57+
dishes = menu.stream().collect(
58+
ArrayList::new,
59+
List::add,
60+
List::addAll);
61+
System.out.println(dishes);
62+
}
63+
}

0 commit comments

Comments
 (0)