Skip to content

Commit

Permalink
Add random number generation and map iteration tests
Browse files Browse the repository at this point in the history
Extended SummarizingDemo with random number generation and statistics computation. Introduced a new test in FileFilterTest to demonstrate iteration over a map using both pre-Java 8 and Java 8+ approaches.
  • Loading branch information
kousen committed Sep 17, 2024
1 parent 937c377 commit b4e217c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/SummarizingDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@ public static void main(String[] args) {
System.out.println(stats.getAverage());
System.out.println(stats.getMax());
System.out.println(stats.getSum());

System.out.println();
DoubleStream.generate(Math::random)
.limit(10)
.forEach(System.out::println);

System.out.println();
stats = DoubleStream.generate(Math::random)
.limit(10)
.peek(x -> System.out.println("before mapping " + x))
.map(x -> x * 100)
.peek(x -> System.out.println("after mapping " + x))
.summaryStatistics();
System.out.println(stats);
}
}
12 changes: 12 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.FileFilter;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -79,4 +80,15 @@ void listJavaSrcFiles_filenameFilter() {
assert javaFiles != null;
assertEquals(8, javaFiles.length);
}

@Test
void iterateOverMap() {
var map = Map.of("a", 1, "b", 2, "c", 3);
// Before Java 8:
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

map.forEach((k, v) -> System.out.println(k + " -> " + v));
}
}

0 comments on commit b4e217c

Please sign in to comment.