diff --git a/src/main/java/lazy/LazyStreams.java b/src/main/java/lazy/LazyStreams.java index d7b8f28..fbef070 100644 --- a/src/main/java/lazy/LazyStreams.java +++ b/src/main/java/lazy/LazyStreams.java @@ -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()); @@ -13,8 +10,8 @@ public static int multByTwo(int n) { } public static boolean modByThree(int n) { - System.out.printf("Inside modByThree with arg %d on thread %s%n", n, - Thread.currentThread().getName()); + System.out.printf("Inside modByThree with arg %d on thread %s%n", + n, Thread.currentThread().getName()); return n % 3 == 0; } @@ -30,8 +27,8 @@ public static void main(String[] args) { // Demonstrate laziness using print statements firstEvenDoubleDivBy3 = IntStream.rangeClosed(100, 2_000_000) // .parallel() - .filter(LazyStreams::modByThree) .map(LazyStreams::multByTwo) + .filter(LazyStreams::modByThree) .findFirst().orElse(0); System.out.printf("First even divisible by 3 is %d%n", firstEvenDoubleDivBy3); } diff --git a/src/test/java/streams/StringExercises.java b/src/test/java/streams/StringExercises.java index dc4c23b..238b084 100644 --- a/src/test/java/streams/StringExercises.java +++ b/src/test/java/streams/StringExercises.java @@ -2,20 +2,23 @@ import org.junit.jupiter.api.Test; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; +import java.util.logging.Logger; import java.util.stream.Collectors; +import static org.assertj.core.api.Assertions.assertThat; + public class StringExercises { private final List strings = Arrays.asList("this", "is", "a", "list", "of", "strings"); + @SuppressWarnings("Convert2Lambda") @Test public void stringLengthSort_InnerClass() { // Java 5, 6, 7 - strings.sort(new Comparator() { + strings.sort(new Comparator<>() { @Override public int compare(String s1, String s2) { return s1.length() - s2.length(); @@ -24,6 +27,7 @@ public int compare(String s1, String s2) { System.out.println(strings); } + @SuppressWarnings("ComparatorCombinators") @Test public void stringLengthSort_lambda() { // Use lambda for the Comparator (reverse sort) @@ -42,40 +46,78 @@ private int compareStrings(String s1, String s2) { return s1.length() - s2.length(); } + @SuppressWarnings("Convert2MethodRef") @Test // Use a lambda that calls 'compareStrings' directly public void stringLengthSort_methodCall() { - + List sorted = strings.stream() + .sorted((s1, s2) -> compareStrings(s1, s2)) + .collect(Collectors.toList()); + System.out.println(sorted); } @Test // Use a method ref to 'compareStrings' public void stringLengthSort_methodRef() { - + List sorted = strings.stream() + .sorted(this::compareStrings) + .collect(Collectors.toList()); + System.out.println(sorted); } @Test // Use Comparator.comparingInt public void stringLengthSort_comparingInt() { - + List sorted = strings.stream() + .sorted(Comparator.comparingInt(String::length) + .thenComparing(Comparator.naturalOrder())) + .collect(Collectors.toList()); + System.out.println(sorted); } @Test public void demoCollectors() { // Get only strings of even length // Add them to a LinkedList + LinkedList evens = strings.stream() + .filter(s -> s.length() % 2 == 0) + .collect(Collectors.toCollection(LinkedList::new)); + System.out.println(evens); + assertThat(evens).isExactlyInstanceOf(LinkedList.class); // Add the strings to a map of string to length + Map map = strings.stream() + // .collect(Collectors.toMap(s -> s, String::length)); + .collect(Collectors.toMap(Function.identity(), String::length)); + System.out.println(map); // Filter out nulls, then print even-length strings + List stringsWithNulls = Arrays.asList("this", null, "is", null, + "a", null, "list", null, "of", null, "strings"); + List evenLengths = stringsWithNulls.stream() + //.filter(s -> s != null && s.length() % 2 == 0) // short-circuiting logical AND + //.filter(s -> s != null) + .filter(Objects::nonNull) + .filter(s -> s.length() % 2 == 0) + .collect(Collectors.toList()); + System.out.println(evenLengths); + + Logger logger = Logger.getLogger("demoCollectors"); // Function composition + Predicate nonNull = Objects::nonNull; + Predicate evenLength = s -> s.length() % 2 == 0; + + Consumer log = logger::info; + Consumer print = System.out::println; // Combine the two predicates and use the result to print non-null, even-length strings + stringsWithNulls.stream() + .filter(nonNull.and(evenLength)) // function composition + .forEach(log.andThen(print)); // function composition - // f: A -> B, g: B -> C, (g.f)(x) = g(f(x)), A -> C + strings.forEach(s -> functionThenConsumer(String::length, print, s)); } - // generated by GitHub Copilot - private Function compose(Function f, Function g) { - return x -> g.apply(f.apply(x)); + private void functionThenConsumer(Function f, Consumer c, T t) { + c.accept(f.apply(t)); } }