MODULE 1 · TOPIC 8 · VISUAL PROGRAMMING LAB

Streams API

Follow data through lazy filter and map operations into terminal results.

Browser-based simulations: change the inputs, trace the Java statements and inspect results. No arbitrary Java execution, real database, threads or file changes occur here.

1. Filter, map and collect pipeline

Watch values enter and leave each stage.

Java example
Program / simulation outputConsole
Data operationsCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Intermediate operations are lazy; terminal operation triggers processing. This visualization groups by stage for clarity; sequential streams may fuse filter and map per element. toList returns an unmodifiable list.

Try and observe

Set threshold 100; see empty output. Change bonus and compare.

2. Reduce — aggregate a shopping cart

Track accumulator and next element.

Java example
Program / simulation outputConsole
Memory / data inspectorCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

reduce combines values using an associative operation and an identity. Summation is a standard reduction.

Try and observe

Set all prices to 0, then 120,80,50.

3. Distinct, sorted and limit

Observe stateful operations and ordering.

Java example
Program / simulation outputConsole
Data operationsCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

distinct removes equal duplicates; sorted orders elements; limit short-circuits the result. Streams cannot be reused after a terminal operation.

Try and observe

Compare descending and ascending with limit=2.

4. Hands-on practice and assessment

Programming challenge

Given a list of employees, filter salaries above a threshold, map to names and collect. Calculate total salary with mapToInt().sum().

Edit the scratchpad below. Copy your code into an actual Java project or IDE to compile and test it.

Quick assessment

Key points and real Java notes

Prefer stateless, non-interfering stream operations. Parallel streams require safe associative reductions and are not automatically faster.