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