Skip to content

richardahasting/javaSnippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Java Snippets

A collection of Java code snippets demonstrating modern Java features and APIs. Each subdirectory is a self-contained project exploring a specific topic.

Contents

Streamers

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() and sum() 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

Source files

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)

Prerequisites

  • 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.

Building and running

With Apache Ant

cd Streamers
ant clean jar
java -jar dist/Streamers.jar

To run the Streamer1 example directly:

cd Streamers
ant compile
java -cp build/classes streamers.Streamer1

With javac (no build tool)

cd Streamers/src
javac streamers/*.java
java streamers.Streamer1

With NetBeans

Open the Streamers directory as a NetBeans project and use Run (F6).

Author

Richard Hasting

About

Streaming and Lambda practice

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages