TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Java Streams

๐Ÿช Java Streams: cheat sheet

๐ŸŽจ The analogyA cookie factory's conveyor belt: stations sort and ice the cookies, and nothing moves until someone at the end asks.
๐ŸŽ“ The regular explanationA lazy pipeline over a source of elements. Intermediate operations such as filter and map describe the work, and a single terminal operation such as collect or findFirst runs it, passing elements through one at a time.
One belt, three kinds of partStep 4 of 4
Cookie boxjamkey iced jam: jam0nutkey iced nut: nut123๐ŸชCookie traystill full๐Ÿ”Sorting station๐Ÿ–Œ๏ธIcing station๐Ÿง‘โ€๐ŸณPackerbox closed
  1. The jam cookie leaves the tray. It's whole, so the sorting station lets it through, the icing station paints it, and the packer puts it in the box.
  2. The oat cookie is broken. The sorting station stops it, so it never reaches the icing station or the box.
  3. The nut cookie is whole. Through it goes, gets its icing and lands in the box next to the jam one.
  4. No more cookies are coming, so the packer closes the box and hands it over. Two iced cookies in the box, and the tray still holds all its cookies.
Lazy, one at a time, and stops earlyStep 6 of 6
๐ŸชCookie traynut and choc stay๐Ÿ”Sorting station๐Ÿ–Œ๏ธIcing station๐Ÿ˜‹Tastergot one
  1. The stations are set up, but nobody has asked for anything yet. Not a single cookie moves.
  2. The taster arrives and asks for one iced cookie. Only now does the belt start.
  3. The first cookie, oat, is broken. The sorting station stops it before the icing station ever sees it.
  4. Next comes jam. It travels the whole belt, gets iced and reaches the taster, before any other cookie leaves the tray.
  5. The taster has their cookie, so the belt stops. The nut and choc cookies never leave the tray.
  6. The taster asks the same belt for another one. That belt run is over, so the answer is no. A new belt is needed.
Packing rules and the duplicate keyStep 4 of 4
๐ŸชBelt๐Ÿง‘โ€๐ŸณPackertaken? add up๐Ÿ“ Jam slot2๐ŸŒพ Oat slot1
  1. The packer's rule is one slot per flavour. A jam cookie arrives and goes in the jam slot.
  2. An oat cookie arrives and gets its own slot.
  3. A second jam cookie! The jam slot is already taken, and nobody told the packer what to do, so the whole belt stops.
  4. Start again, this time with a rule up front (if the slot is taken, add to the count). The second jam cookie makes it 2.
Boxed numbers and the number beltStep 4 of 4
๐ŸชCookie trayempty๐Ÿ“ฆOrdinary beltbox each one๐Ÿง‘โ€๐ŸณUnpackertotal 55๐Ÿ”ขNumber belt๐ŸงฎAdder with buttonsno average
  1. On the ordinary belt, every weight card is put in its own little box before it can ride.
  2. At the end, someone unpacks every box before adding the weights up. Same total, lots of boxes.
  3. On the number belt the cards ride bare. The adder just presses the add-up button.
  4. With an empty tray, the add-up button says 0, but the average button says there's no average, rather than making one up.
Parallel streams and the shared crewStep 4 of 4
๐Ÿ‘ฅ Shared crew๐ŸชCookie tray๐Ÿ‘ทCrew member 1๐Ÿ‘ทCrew member 2๐Ÿ“ Wall notepad๐Ÿง‘โ€๐ŸณPackerin order๐Ÿ™‹Another teamwaiting for crew
  1. The cookies are split in two, and each half goes to a worker from the factory's shared crew.
  2. Both workers scribble on the wall notepad at the same moment. The notes get smudged and one is lost.
  3. The right way is to let the packer gather each worker's batch and put them back in the original order.
  4. Meanwhile another team needs the crew too. If our workers are stuck waiting on something slow, that team waits as well.

๐Ÿงฉ Key questions

The sorting and icing stations are set up, but nobody is at the end of the belt. How many cookies have been iced?
None, because nothing moves until someone at the end asks. Setting up stations does no work. The belt only starts when someone at the end asks for something.
The taster asks for one iced cookie. The first cookie is broken, the second is whole. What happens to the third cookie?
It never leaves the tray. Each cookie goes all the way down the belt before the next one starts, and the belt stops as soon as the taster has one.
The packer puts cookies on a shelf with one slot per flavour, with no rule for taken slots. Two jam cookies arrive. What happens?
The packer stops the belt, because nobody said what to do. Give the packer a rule up front, such as adding to the count, and two jam cookies are fine.
Two crew members both scribble results on the wall notepad at the same time. What's the better way?
Let the packer gather each worker's batch at the end. When two people write on one notepad at once, notes get lost. The packer joins the batches safely, in order.
Your job has the crew phoning a slow supplier for every cookie. Why does that hurt other teams?
The crew is shared by the whole factory, so waiting workers leave nobody for anyone else. The crew is shared, so every minute a worker spends waiting is a minute no other team can use them.