|
| 1 | +package com.brianway.learning.java8.streamapi; |
| 2 | + |
| 3 | +import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH; |
| 4 | +import static java.util.stream.Collectors.partitioningBy; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.EnumSet; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.function.BiConsumer; |
| 14 | +import java.util.function.BinaryOperator; |
| 15 | +import java.util.function.Function; |
| 16 | +import java.util.function.Predicate; |
| 17 | +import java.util.function.Supplier; |
| 18 | +import java.util.stream.Collector; |
| 19 | +import java.util.stream.IntStream; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +/** |
| 23 | + * 分区:分类的特殊情况。分区函数返回一个布尔值 |
| 24 | + */ |
| 25 | +public class PartitionPrimeNumbers { |
| 26 | + |
| 27 | + public static void main(String... args) { |
| 28 | + System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100)); |
| 29 | + System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithCustomCollector(100)); |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static Map<Boolean, List<Integer>> partitionPrimes(int n) { |
| 34 | + return IntStream.rangeClosed(2, n).boxed() |
| 35 | + .collect(partitioningBy(PartitionPrimeNumbers::isPrime)); |
| 36 | + } |
| 37 | + |
| 38 | + public static boolean isPrime(int candidate) { |
| 39 | + return IntStream.rangeClosed(2, candidate - 1) |
| 40 | + .limit((long) Math.floor(Math.sqrt((double) candidate)) - 1) |
| 41 | + .noneMatch(i -> candidate % i == 0); |
| 42 | + } |
| 43 | + |
| 44 | + public static Map<Boolean, List<Integer>> partitionPrimesWithCustomCollector(int n) { |
| 45 | + return IntStream.rangeClosed(2, n).boxed().collect(new PrimeNumbersCollector()); |
| 46 | + } |
| 47 | + |
| 48 | + public static boolean isPrime(List<Integer> primes, Integer candidate) { |
| 49 | + double candidateRoot = Math.sqrt((double) candidate); |
| 50 | + //return primes.stream().filter(p -> p < candidateRoot).noneMatch(p -> candidate % p == 0); |
| 51 | + return takeWhile(primes, i -> i <= candidateRoot).stream().noneMatch(i -> candidate % i == 0); |
| 52 | + } |
| 53 | + |
| 54 | + public static <A> List<A> takeWhile(List<A> list, Predicate<A> p) { |
| 55 | + int i = 0; |
| 56 | + for (A item : list) { |
| 57 | + if (!p.test(item)) { |
| 58 | + return list.subList(0, i); |
| 59 | + } |
| 60 | + i++; |
| 61 | + } |
| 62 | + return list; |
| 63 | + } |
| 64 | + |
| 65 | + public static class PrimeNumbersCollector |
| 66 | + implements Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> { |
| 67 | + |
| 68 | + @Override |
| 69 | + public Supplier<Map<Boolean, List<Integer>>> supplier() { |
| 70 | + return () -> new HashMap<Boolean, List<Integer>>() {{ |
| 71 | + put(true, new ArrayList<Integer>()); |
| 72 | + put(false, new ArrayList<Integer>()); |
| 73 | + }}; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public BiConsumer<Map<Boolean, List<Integer>>, Integer> accumulator() { |
| 78 | + return (Map<Boolean, List<Integer>> acc, Integer candidate) -> { |
| 79 | + acc.get(isPrime(acc.get(true), |
| 80 | + candidate)) |
| 81 | + .add(candidate); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public BinaryOperator<Map<Boolean, List<Integer>>> combiner() { |
| 87 | + return (Map<Boolean, List<Integer>> map1, Map<Boolean, List<Integer>> map2) -> { |
| 88 | + map1.get(true).addAll(map2.get(true)); |
| 89 | + map1.get(false).addAll(map2.get(false)); |
| 90 | + return map1; |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Function<Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> finisher() { |
| 96 | + return i -> i; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Set<Characteristics> characteristics() { |
| 101 | + return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public Map<Boolean, List<Integer>> partitionPrimesWithInlineCollector(int n) { |
| 106 | + return Stream.iterate(2, i -> i + 1).limit(n) |
| 107 | + .collect( |
| 108 | + () -> new HashMap<Boolean, List<Integer>>() {{ |
| 109 | + put(true, new ArrayList<Integer>()); |
| 110 | + put(false, new ArrayList<Integer>()); |
| 111 | + }}, |
| 112 | + (acc, candidate) -> { |
| 113 | + acc.get(isPrime(acc.get(true), candidate)) |
| 114 | + .add(candidate); |
| 115 | + }, |
| 116 | + (map1, map2) -> { |
| 117 | + map1.get(true).addAll(map2.get(true)); |
| 118 | + map1.get(false).addAll(map2.get(false)); |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
0 commit comments