diff --git a/src/main/java/lambdas/UsePerson.java b/src/main/java/lambdas/UsePerson.java index a231dea..220eb7f 100644 --- a/src/main/java/lambdas/UsePerson.java +++ b/src/main/java/lambdas/UsePerson.java @@ -24,7 +24,7 @@ public static void main(String[] args) { people = names.stream() .map(Person::new) // uses the Person(String) ctr - // .map(Person::new) // uses the Person(Person) ctr + .map(Person::new) // uses the Person(Person) ctr .collect(Collectors.toList()); System.out.println(people); @@ -49,7 +49,7 @@ public static void main(String[] args) { LinkedList linkedPersons = names.stream() .map(Person::new) .collect( - () -> new LinkedList(), // Supplier + () -> new LinkedList<>(), // Supplier (list, person) -> list.add(person), // BiConsumer (list1, list2) -> list1.addAll(list2)); // BiConsumer System.out.println(linkedPersons); diff --git a/src/main/java/lazy/LazyStreams.java b/src/main/java/lazy/LazyStreams.java index a2ee7a6..08ffec4 100644 --- a/src/main/java/lazy/LazyStreams.java +++ b/src/main/java/lazy/LazyStreams.java @@ -18,8 +18,15 @@ public static boolean modByThree(int n) { public static void main(String[] args) { // multiply numbers between 100 and 200 by 2, then find first n divisible by 3 int firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 200) + .peek(n -> System.out.printf("Original: %d%n", n)) + .map(n -> { + System.out.println("Doubling " + n); + return n; + }) .map(n -> n * 2) + .peek(n -> System.out.printf("Doubled: %d%n", n)) .filter(n -> n % 3 == 0) + .peek(n -> System.out.printf("Filtered: %d%n", n)) .findFirst().orElse(0); System.out.println(firstEvenDoubleDivBy3); @@ -27,9 +34,7 @@ public static void main(String[] args) { // Demonstrate laziness using print statements firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 2_000_000) .map(LazyStreams::multByTwo) - .sequential() .filter(LazyStreams::modByThree) - .parallel() .findFirst().orElse(0); System.out.printf("First even divisible by 3 is %d%n", firstEvenDoubleDivBy3); }