Core Java · Chapter 33 of 48

Collectors

By the end of this chapter you can turn a stream into a map of groups, a two-way split, a joined string or a summary of numbers, each with one collect call. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 6 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

Meera has the railways' quarterly review on Friday. The page needs the number of orders at each station, the cups sold of each item in A to Z order, the items of big bills and of small bills, the best bill at each station, every item any train ordered, and the count, sum, smallest and largest bill.

With toList() from the last chapter, every one of these is a stream followed by a loop over a HashMap: get the key, check for null, put a new list or a new count, add. Six maps built by hand.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Reports.java: count the orders at each station

    In Reports.java, make ordersPerStation(orders) return a map from each station to how many orders it took, as a Long. Only stations with orders appear. No orders give an empty map. Any map order.

    For example, the test's quarter has three SBC orders, two NDLS and one CSMT, which gives {SBC=3, NDLS=2, CSMT=1}.

  2. Task 2

    Reports.java: total the cups of each item, sorted by item

    In Reports.java, make cupsPerItem(orders) return a map from each item to the sum of its orders' quantities, with the items in A to Z order when you go through the map.

    For example, the test's quarter gives {Bun maska=3, Cutting chai=12, Masala chai=10}, in exactly that order: Masala chai is 3 + 2 + 5 cups.

  3. Task 3

    Reports.java: split the items into big bills and small bills

    In Reports.java, make bigAndSmall(orders, threshold) return a map with two keys. Under true, the items of the orders whose total is at least threshold; under false, the items of the rest. Each list keeps the orders' order. Both keys are always present, even when a list is empty, and even for no orders.

    For example, with threshold 100 the test's quarter gives true=[Masala chai, Cutting chai, Masala chai, Bun maska] (the bun maska bill of exactly 100 counts as big) and false=[Bun maska, Masala chai]. One bun maska order at threshold 1000 gives true=[] and false=[Bun maska].

  4. Task 4

    Reports.java: keep the best bill of each station with toMap

    In Reports.java, make bestBillPerStation(orders) return a map from each station to the largest order total at that station. A station that appears more than once must not throw. Any map order.

    For example, the test's quarter gives {SBC=300, NDLS=200, CSMT=80}: SBC's bills were 120, 300 and 100. A single NDLS order of 50 gives {NDLS=50}.

  5. Task 5

    Reports.java: flatten every train's order into one line

    In Reports.java, make everythingOrdered(trainOrders) return every item from every train's list, each item once, sorted A to Z, joined into one string with a comma and a space between items. Empty lists add nothing, and no trains give the empty string "".

    For example, [[Masala chai, Bun maska], [Masala chai], [], [Parle-G, Cutting chai, Bun maska]] gives "Bun maska, Cutting chai, Masala chai, Parle-G".

  6. Task 6

    Reports.java: the count, sum, smallest and largest bill in one pass

    In Reports.java, make billStats(orders) return an IntSummaryStatistics of the order totals, so its count, sum, min, max and average are all right. No orders give a count of 0.

    For example, the test's quarter gives count 6, sum 850, min 50, max 300 and an average of 850 divided by 6.

What you will learn

  • groupingBy: a map from each key to its group, and a downstream collector that turns each group into a count, a sum or a list.
  • partitioningBy: a split into true and false, with both keys always there.
  • toMap: one entry per element, and what to do when keys repeat.
  • flatMap, joining and summarizingInt: flatten lists, glue strings, and get five numbers in one pass.

More in Core Java

Build "Collectors" 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.