| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 56ee0b9 commit 55700d1
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,10 +11,10 @@ public class BestPriceFinderMain { | |||
| 11 | 11 | private static BestPriceFinder bestPriceFinder = new BestPriceFinder(); | |
| 12 | 12 | ||
| 13 | 13 | public static void main(String[] args) { | |
| 14 | - execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone")); | ||
| 15 | - execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone")); | ||
| 14 | + //execute("sequential", () -> bestPriceFinder.findPriceSequential("myPhone")); | ||
| 15 | + //execute("parallel", () -> bestPriceFinder.findPriceParallel("myPhone")); | ||
| 16 | 16 | execute("composed CompletableFuture", () -> bestPriceFinder.findPrice("myPhone")); | |
| 17 | - bestPriceFinder.printPricesStream(); | ||
| 17 | + //bestPriceFinder.printPricesStream(); | ||
| 18 | 18 | } | |
| 19 | 19 | ||
| 20 | 20 | private static void execute(String msg, Supplier<List<String>> s) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,8 @@ public class Util { | |||
| 11 | 11 | private static final DecimalFormat formatter = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.US)); | |
| 12 | 12 | ||
| 13 | 13 | public static void delay() { | |
| 14 | - //int delay = 1000; | ||
| 15 | - int delay = 500 + RANDOM.nextInt(2000); | ||
| 14 | + int delay = 1000; | ||
| 15 | + //int delay = 500 + RANDOM.nextInt(2000); | ||
| 16 | 16 | try { | |
| 17 | 17 | Thread.sleep(delay); | |
| 18 | 18 | } catch (InterruptedException e) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | public class CollectorHarness { | |
| 8 | 8 | ||
| 9 | 9 | public static void main(String[] args) { | |
| 10 | - System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs"); | ||
| 10 | + //System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs"); | ||
| 11 | 11 | System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" ); | |
| 12 | 12 | } | |
| 13 | 13 | ||
@@ -18,6 +18,7 @@ private static long execute(Consumer<Integer> primePartitioner) { | |||
| 18 | 18 | primePartitioner.accept(1_000_000); | |
| 19 | 19 | long duration = (System.nanoTime() - start) / 1_000_000; | |
| 20 | 20 | if (duration < fastest) fastest = duration; | |
| 21 | + System.out.println("done in " + duration); | ||
| 21 | 22 | } | |
| 22 | 23 | return fastest; | |
| 23 | 24 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,23 +17,24 @@ public static void main(String ... args) { | |||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | public static Map<Boolean, List<Integer>> partitionPrimes(int n) { | |
| 20 | - return Stream.iterate(2, i -> i + 1).limit(n-1) | ||
| 20 | + return IntStream.rangeClosed(2, n).boxed() | ||
| 21 | 21 | .collect(partitioningBy(candidate -> isPrime(candidate))); | |
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | 24 | public static boolean isPrime(int candidate) { | |
| 25 | - return Stream.iterate(2, i -> i + 1) | ||
| 25 | + return IntStream.rangeClosed(2, candidate-1) | ||
| 26 | 26 | .limit((long) Math.floor(Math.sqrt((double) candidate)) - 1) | |
| 27 | 27 | .noneMatch(i -> candidate % i == 0); | |
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | 30 | public static Map<Boolean, List<Integer>> partitionPrimesWithCustomCollector(int n) { | |
| 31 | - return Stream.iterate(2, i -> i + 1).limit(n-1).collect(new PrimeNumbersCollector()); | ||
| 31 | + return IntStream.rangeClosed(2, n).boxed().collect(new PrimeNumbersCollector()); | ||
| 32 | 32 | } | |
| 33 | 33 | ||
| 34 | 34 | public static boolean isPrime(List<Integer> primes, Integer candidate) { | |
| 35 | 35 | double candidateRoot = Math.sqrt((double) candidate); | |
| 36 | - return takeWhile(primes, i -> i <= candidateRoot).stream().noneMatch(i -> candidate % i == 0); | ||
| 36 | + return primes.stream().filter(p -> p < candidateRoot).noneMatch(p -> candidate % p == 0); | ||
| 37 | + //return takeWhile(primes, i -> i <= candidateRoot).stream().noneMatch(i -> candidate % i == 0); | ||
| 37 | 38 | } | |
| 38 | 39 | ||
| 39 | 40 | public static <A> List<A> takeWhile(List<A> list, Predicate<A> p) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | import java.util.function.*; | |
| 5 | 5 | import java.util.stream.*; | |
| 6 | 6 | ||
| 7 | + import static java.util.Comparator.comparingInt; | ||
| 7 | 8 | import static java.util.stream.Collectors.*; | |
| 8 | 9 | import static lambdasinaction.chap5.Dish.menu; | |
| 9 | 10 | ||
@@ -27,7 +28,7 @@ private static Object mostCaloricPartitionedByVegetarian() { | |||
| 27 | 28 | return menu.stream().collect( | |
| 28 | 29 | partitioningBy(Dish::isVegetarian, | |
| 29 | 30 | collectingAndThen( | |
| 30 | - reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), | ||
| 31 | + maxBy(comparingInt(Dish::getCalories)), | ||
| 31 | 32 | Optional::get))); | |
| 32 | 33 | } | |
| 33 | 34 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments