Core Java · Chapter 28 of 48
Sets and maps
By the end of this chapter you can keep things once with a set, look up a value by its key with a map, and pick the set or map whose order you need. 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 the end of the day Ravi has a long list of sales: "Masala chai, SBC,
- Bun maska, NDLS, 50. Masala chai, SBC, 40." He wants answers, not rows: which items were sold, which stations sold anything, how much each station took and what each one sold. And CSMT wants to know which train comes next.
With lists alone, every answer is a loop inside a loop: to drop a repeat you first search the list for it, and to total a station you keep a list of codes beside a list of sums and hope their indexes stay in step. One slip puts a sum against the wrong station.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Ledger.java: each item once, in first-ordered order
In Ledger.java, make uniqueInOrder(items) return a Set holding each item once, in the order it first appears in the list. Repeats are dropped. An empty list gives an empty set.
For example, Masala chai, Bun maska, Masala chai, Cutting chai, Bun maska gives Masala chai, Bun maska, Cutting chai, in that order when you loop over the set.
- Task 2
Ledger.java: the stations that sold anything, sorted
In Ledger.java, make stationsServed(sales) return a SortedSet of the station codes that have at least one sale, each once, sorted A to Z, so its first() is the earliest code. No sales gives an empty set.
For example, sales at SBC, NDLS, SBC, CSMT, SBC and NDLS give CSMT, NDLS, SBC, and first() is CSMT.
- Task 3
Ledger.java: add up the takings of each station
In Ledger.java, make revenueByStation(sales) return a Map from each station code to the total amount of its sales, in rupees. Every station with a sale appears once. Any kind of map and any order. No sales gives an empty map.
For example, SBC 40, NDLS 50, SBC 25, CSMT 40, SBC 40 and NDLS 25 give SBC 105, NDLS 75 and CSMT 40.
- Task 4
Ledger.java: group the items sold at each station
In Ledger.java, make itemsByStation(sales) return a Map from each station code to the list of items sold there, in the order they were sold, repeats kept. The map's keys come out sorted A to Z.
For example, the six sales above give CSMT: Masala chai; NDLS: Bun maska, Cutting chai; SBC: Masala chai, Cutting chai, Masala chai, with the keys in the order CSMT, NDLS, SBC.
- Task 5
Timetable.java: find the next and the last train
In Timetable.java, write three methods on the trains TreeMap. add(time, train) stores the train under its arrival time; trains may be added in any order. nextTrain(now) returns the name of the first train arriving at or after now, or null when there is none. lastTrain(now) returns the name of the last train arriving at or before now, or null when there is none. A train arriving exactly at now is both the next and the last.
For example, with Shatabdi at 06:10, Udyan Express at 07:45 and Rajdhani at 09:30: at 06:30 the next is Udyan Express and the last is Shatabdi; at 07:45 both are Udyan Express; at 05:00 the next is Shatabdi and the last is null; at 22:00 the next is null and the last is Rajdhani.
What you will learn
- Sets: each element once; HashSet, LinkedHashSet and TreeSet differ only in the order you get them back.
- Maps: a value for each key, with put, get and getOrDefault.
- merge and computeIfAbsent: update a key's value in one call, whether the key is new or not.
- TreeMap: keys kept sorted, so it can find the nearest key.
More in Core Java
Build "Sets and maps" 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.