Core Java · Chapter 32 of 48
The Stream API
By the end of this chapter you can answer a question about a list in one pipeline: keep some elements, turn them into something else, sort, count, add up, and say when each step really runs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Core Java
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
At ten at night Ravi wants five answers about the SBC shift: which items were sold, once each and A to Z; the biggest bills; the takings and the average bill; how many orders came from one station; and whether anyone ordered in bulk.
With what you know so far, each answer is a loop with a temporary list, an if and a counter. The items need a contains check before every add and a sort afterwards. The biggest bills need a copy of the list first, or sorting reorders the shift itself.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Sales.java: the items a station sold, once each, A to Z
In Sales.java, make itemsSoldAt(orders, station) return the items of the orders from that station, each item once, sorted A to Z. A station with no orders returns an empty list.
For example, in the test shift SBC sold Masala chai, Cutting chai, Masala chai and Bun maska, which returns [Bun maska, Cutting chai, Masala chai]; NDLS returns [Bun maska]; HWH returns [].
- Task 2
Sales.java: the biggest orders of the shift
In Sales.java, make biggest(orders, n) return the n orders with the largest total, largest first. Orders with equal totals keep their order from the shift. When n is more than the number of orders, return them all, sorted; when n is 0, return an empty list. The orders list itself must not change.
For example, in the test shift the two orders of 300 (12 cutting chai at 25, then 6 bun maska at 50, in shift order) come first and 3 masala chai at 40 (120) third, so biggest(shift, 3) returns those three orders and biggest(shift, 1) returns only the 12 cutting chai.
- Task 3
Sales.java: the takings and the average bill
In Sales.java, make revenue(orders) return the sum of every order's total, in rupees, and averageBill(orders) return the average of the orders' totals as a double, not rounded down. With no orders, revenue returns 0 and averageBill returns 0.0.
For example, totals of 120 and 50 give a revenue of 170 and an average bill of 85.0; the whole test shift takes 860.
- Task 4
Sales.java: count orders and ask yes or no questions
In Sales.java, write three methods. countAt(orders, station) returns how many orders came from that station, 0 for none. hasBulkOrder(orders, minQty) returns true when at least one order has minQty cups or more (exactly minQty counts), and false for no orders. allFrom(orders, station) returns true when every order came from that station, and true for no orders at all.
For example, in the test shift countAt SBC is 4 and HWH is 0; hasBulkOrder is true for 10 and 12 (the 12 cutting chai) but false for 13; allFrom SBC is false for the shift and true for an empty list.
- Task 5 · interview question
Interview: Sales.java, predict what a lazy stream prints and when
In Sales.java, make predictLazy() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- A pipeline: a source, steps such as filter, map, distinct, sorted and limit, then one terminal operation that gives a result.
- Number streams: mapToInt turns objects into an IntStream with sum and average.
- Yes or no questions: count, anyMatch and allMatch, and what they say about an empty list.
- Laziness: nothing runs until the terminal operation asks.
More in Core Java
Build The Stream API yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.