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