| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,13 +20,17 @@ | |||
| 20 | 20 | import java.util.stream.Stream; | |
| 21 | 21 | ||
| 22 | 22 | /** | |
| 23 | - * 分区:分类的特殊情况。分区函数返回一个布尔值 | ||
| 23 | + * 得到 100 以内的质数和非质数 | ||
| 24 | + * | ||
| 25 | + * - 分区:分类的特殊情况。分区函数返回一个布尔值 | ||
| 26 | + * - Collector | ||
| 24 | 27 | */ | |
| 25 | 28 | public class PartitionPrimeNumbers { | |
| 26 | 29 | ||
| 27 | 30 | public static void main(String... args) { | |
| 28 | 31 | System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100)); | |
| 29 | 32 | 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)); | ||
| 30 | 34 | ||
| 31 | 35 | } | |
| 32 | 36 | ||
@@ -62,22 +66,29 @@ public static <A> List<A> takeWhile(List<A> list, Predicate<A> p) { | |||
| 62 | 66 | return list; | |
| 63 | 67 | } | |
| 64 | 68 | ||
| 69 | + /** | ||
| 70 | + * Collector | ||
| 71 | + * 1.建立新的结果容器: supplier 方法 | ||
| 72 | + * 2.将元素添到结果容器: accumulator 方法 | ||
| 73 | + * 3.对容器应用最终转换: finisher 方法 | ||
| 74 | + * 4.合并两个结果容器: combiner | ||
| 75 | + * 5.characteristics 方法 | ||
| 76 | + */ | ||
| 65 | 77 | public static class PrimeNumbersCollector | |
| 66 | 78 | implements Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> { | |
| 67 | 79 | ||
| 68 | 80 | @Override | |
| 69 | 81 | public Supplier<Map<Boolean, List<Integer>>> supplier() { | |
| 70 | 82 | 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<>()); | ||
| 73 | 85 | }}; | |
| 74 | 86 | } | |
| 75 | 87 | ||
| 76 | 88 | @Override | |
| 77 | 89 | public BiConsumer<Map<Boolean, List<Integer>>, Integer> accumulator() { | |
| 78 | 90 | 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)) | ||
| 81 | 92 | .add(candidate); | |
| 82 | 93 | }; | |
| 83 | 94 | } | |
@@ -102,17 +113,17 @@ public Set<Characteristics> characteristics() { | |||
| 102 | 113 | } | |
| 103 | 114 | } | |
| 104 | 115 | ||
| 105 | - public Map<Boolean, List<Integer>> partitionPrimesWithInlineCollector(int n) { | ||
| 116 | + public static Map<Boolean, List<Integer>> partitionPrimesWithInlineCollector(int n) { | ||
| 106 | 117 | return Stream.iterate(2, i -> i + 1).limit(n) | |
| 107 | 118 | .collect( | |
| 108 | 119 | () -> 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<>()); | ||
| 111 | 122 | }}, | |
| 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 | + , | ||
| 116 | 127 | (map1, map2) -> { | |
| 117 | 128 | map1.get(true).addAll(map2.get(true)); | |
| 118 | 129 | map1.get(false).addAll(map2.get(false)); | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments