Skip to content

Commit

Permalink
Improve code readability and debug visibility in multiple classes
Browse files Browse the repository at this point in the history
In `UsePerson.java` and `RandomDemo.java`, print statements were added to provide more visibility during debug. In `FlatMapDemo.java`, replaced `size() == 0` with `isEmpty()` for checking empty collections, improving code readability and performance. Added a new generator in `RandomDemo.java`, showing usage of the `DoubleStream` with the `Math::random` method.
  • Loading branch information
kousen committed Oct 3, 2023
1 parent f702158 commit 21dec38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/main/java/RandomDemo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import java.util.OptionalInt;
import java.util.Random;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;

public class RandomDemo {
@SuppressWarnings("SimplifyStreamApiCallChains")
public static void main(String[] args) {
DoubleStream.generate(Math::random)
.limit(10)
.forEach(System.out::println);

Random r = new Random();
int sum = IntStream.generate(() -> r.nextInt(10))
.limit(10)
.map(n -> {
.map(n -> { // this is the peek method
System.out.println("n = " + n);
return n;
})
Expand All @@ -22,6 +28,7 @@ public static void main(String[] args) {
.peek(System.out::println)
.filter(n -> n > 7)
.findFirst();
System.out.println(first);

}
}
1 change: 1 addition & 0 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void main(String[] args) {
// .map(Person::new) // uses the Person(Person) ctr -> Stream<Person>
.collect(Collectors.toList());
System.out.println(people);
System.out.println(people.getClass().getName());

Person[] peopleArray = names.stream()
.map(Person::new)
Expand Down
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 21dec38

Please sign in to comment.