A collection of Java code snippets demonstrating modern Java features and APIs. Each subdirectory is a self-contained project exploring a specific topic.
Demonstrates the Java Streams API and lambda expressions by comparing traditional imperative approaches with their modern functional equivalents. The snippets cover:
- Filtering -- Using
Stream.filter()to select elements by criteria (e.g., removing words of a specific length) - Counting -- Using
Stream.count()for element counting vs. manual loop counters - Mapping and reducing -- Using
mapToInt()andsum()to aggregate values - Parallel streams -- Using
parallelStream()for concurrent processing - Array-to-List conversion -- Populating collections from arrays using
Arrays.stream().forEach()vs. traditional loops - Side-by-side comparison -- Each operation is implemented both the "old way" (imperative loops) and the "new way" (streams and lambdas) so the differences are clear
| File | Description |
|---|---|
DataListStrings.java |
Provides a sample dataset (an array of strings) and demonstrates two ways to populate an ArrayList from an array |
Streamer1.java |
Filters, counts, and collects strings using both imperative loops and stream operations |
Streamers.java |
Main class entry point (scaffold for additional examples) |
- Java 15 or later (the project targets
javac.source=15) - Apache Ant (the project uses a NetBeans-generated Ant build)
Alternatively, you can open the project directly in NetBeans IDE, which will handle building and running automatically.
cd Streamers
ant clean jar
java -jar dist/Streamers.jarTo run the Streamer1 example directly:
cd Streamers
ant compile
java -cp build/classes streamers.Streamer1cd Streamers/src
javac streamers/*.java
java streamers.Streamer1Open the Streamers directory as a NetBeans project and use Run (F6).
Richard Hasting