diff --git a/src/test/java/streams/StringExercises.java b/src/test/java/streams/StringExercises.java deleted file mode 100644 index 9ac9e25..0000000 --- a/src/test/java/streams/StringExercises.java +++ /dev/null @@ -1,72 +0,0 @@ -package streams; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.function.Function; - -public class StringExercises { - private final List strings = Arrays.asList("this", "is", "a", - "list", "of", "strings"); - - @Test - public void stringLengthSort_InnerClass() { // Java 5, 6, 7 - strings.sort(new Comparator() { - @Override - public int compare(String s1, String s2) { - return s1.length() - s2.length(); - } - }); - System.out.println(strings); - } - - @Test - public void stringLengthSort_lambda() { - // Use lambda for the Comparator (reverse sort) - - // Use the "sorted" method on Stream - } - - private int compareStrings(String s1, String s2) { - return s1.length() - s2.length(); - } - - @Test // Use a lambda that calls 'compareStrings' directly - public void stringLengthSort_methodCall() { - - } - - @Test // Use a method ref to 'compareStrings' - public void stringLengthSort_methodRef() { - - } - - @Test // Use Comparator.comparingInt - public void stringLengthSort_comparingInt() { - - } - - @Test - public void demoCollectors() { - // Get only strings of even length - // Add them to a LinkedList - - // Add the strings to a map of string to length - - // Filter out nulls, then print even-length strings - - // Function composition - - // Combine the two predicates and use the result to print non-null, even-length strings - - // f: A -> B, g: B -> C, (g.f)(x) = g(f(x)), A -> C - } - - // generated by GitHub Copilot - private Function compose(Function f, Function g) { - return x -> g.apply(f.apply(x)); - } - -} diff --git a/src/test/java/streams/StringExercisesTest.java b/src/test/java/streams/StringExercisesTest.java new file mode 100644 index 0000000..b3615be --- /dev/null +++ b/src/test/java/streams/StringExercisesTest.java @@ -0,0 +1,125 @@ +package streams; + +import org.junit.jupiter.api.Test; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringExercisesTest { + private final List strings = Arrays.asList("this", "is", "a", + "list", "of", "strings"); + + @Test + void sortByLength_collectons_sort() { + // Destructive sort -- original list is modified + Collections.sort(strings, new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.length() - s2.length(); + } + }); + System.out.println(strings); + } + + @Test + public void stringLengthSort_InnerClass() { // Java 5, 6, 7 + strings.sort(new Comparator() { + @Override + public int compare(String s1, String s2) { + return s1.length() - s2.length(); + } + }); + System.out.println(strings); + } + + @Test + public void stringLengthSort_lambda() { + // Use lambda for the Comparator (reverse sort) + strings.sort((s1, s2) -> s2.length() - s1.length()); + System.out.println(strings); + + // Use the "sorted" method on Stream + List sortedStrings = strings.stream() + .sorted((s1, s2) -> s1.length() - s2.length()) + .toList(); + System.out.println("Sorted: " + sortedStrings); + System.out.println("Original: " + strings); + } + + private int compareStrings(String s1, String s2) { + return s1.length() - s2.length(); + } + + @Test // Use a lambda that calls 'compareStrings' directly + public void stringLengthSort_methodCall() { + List sortedStrings = strings.stream() + .sorted((s1, s2) -> compareStrings(s1, s2)) + .toList(); + System.out.println("Sorted: " + sortedStrings); + } + + @Test // Use a method ref to 'compareStrings' + public void stringLengthSort_methodRef() { + List sortedStrings = strings.stream() + .sorted(this::compareStrings) + .toList(); + System.out.println("Sorted: " + sortedStrings); + assertThat(sortedStrings.stream().map(String::length).toList()) + .containsExactly(1, 2, 2, 4, 4, 7); + } + + @Test // Use Comparator.comparingInt + public void stringLengthSort_comparingInt() { + List sortedStrings = strings.stream() + .sorted(Comparator.comparingInt(String::length) + .thenComparing(Comparator.naturalOrder())) + .toList(); + System.out.println("Sorted: " + sortedStrings); + assertThat(sortedStrings.stream().map(String::length).toList()) + .containsExactly(1, 2, 2, 4, 4, 7); + } + + @Test + public void demoCollectors() { + // Get only strings of even length + // Turn them into uppercase + // Add them to a LinkedList + LinkedList evens = strings.stream() + .filter(s -> s.length() % 2 == 0) + .map(String::toUpperCase) + .collect(Collectors.toCollection(LinkedList::new)); + System.out.println(evens); + + // 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 stringWithNulls = Arrays.asList("this", null, "is", + null, "a", "list", "of", null, "strings", null); + List evenLengthList = stringWithNulls.stream() + // .filter(s -> s != null && s.length() % 2 == 0) + // .filter(s -> s != null) + .filter(Objects::nonNull) + .filter(s -> s.length() % 2 == 0) + .toList(); + System.out.println(evenLengthList); + + // Function composition + + // Combine the two predicates and use the result to print non-null, even-length strings + + // f: A -> B, g: B -> C, (g.f)(x) = g(f(x)), A -> C + } + + // generated by GitHub Copilot + private Function compose(Function f, Function g) { + return x -> g.apply(f.apply(x)); + } + +}