Skip to content

Commit

Permalink
Refactor LazyStreams class and update SummarizingDemo
Browse files Browse the repository at this point in the history
Removed unused logger from LazyStreams class and rearranged order of stream operations for better readability. Modified behavior for handling empty streams in FlatMapDemo. Added functionality to generate a list of random doubles in SummarizingDemo. Also, made miscellaneous changes to improve code readability and maintainability.
  • Loading branch information
kousen committed Feb 6, 2024
1 parent fd28f25 commit ecd21bb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/main/java/SummarizingDemo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.DoubleStream;

public class SummarizingDemo {
public static void main(String[] args) {
List<Double> collect = DoubleStream.generate(Math::random)
.limit(10)
.boxed()
.toList();
System.out.println(collect);


DoubleSummaryStatistics stats = DoubleStream.generate(Math::random)
.limit(1_000_000)
.summaryStatistics();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.Collectors;

public class UsePerson {
@SuppressWarnings("Convert2MethodRef")
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");

Expand All @@ -26,8 +27,7 @@ public static void main(String[] args) {
people = names.stream()
.map(Person::new) // uses the Person(String) ctr
.peek(person -> System.out.println(person + " processed by " +
Thread.currentThread()
.getName()))
Thread.currentThread().getName()))
// .map(Person::new) // uses the Person(Person) ctr
.collect(Collectors.toList());
System.out.println(people);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/lazy/LazyStreams.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package lazy;

import java.util.logging.Logger;
import java.util.stream.IntStream;

public class LazyStreams {
private static final Logger logger = Logger.getLogger(LazyStreams.class.getName());

public static int multByTwo(int n) {
System.out.printf("Inside multByTwo with arg %d on thread %s%n",
n, Thread.currentThread().getName());
Expand All @@ -23,16 +20,16 @@ public static void main(String[] args) {
int firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 200)
.map(n -> n * 2)
.filter(n -> n % 3 == 0)
.findFirst().orElse(0);
.findFirst().orElseThrow();
System.out.println(firstEvenDoubleDivBy3);


// Demonstrate laziness using print statements
firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 2_000_000)
// .parallel()
.filter(LazyStreams::modByThree)
.map(LazyStreams::multByTwo)
.findFirst().orElse(0);
.filter(LazyStreams::modByThree)
.findAny().orElse(0);
System.out.printf("First even divisible by 3 is %d%n", firstEvenDoubleDivBy3);
}
}
2 changes: 1 addition & 1 deletion src/main/java/streams/FlatMapDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void main(String[] args) {
// stream() on an empty collection already returns an empty stream
customers.stream()
.flatMap(customer ->
customer.getOrders().size() == 0 ? Stream.empty() :
customer.getOrders().isEmpty() ? Stream.empty() :
customer.getOrders().stream())
.forEach(System.out::println);

Expand Down

0 comments on commit ecd21bb

Please sign in to comment.