๐ช 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.
- 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.
- The oat cookie is broken. The sorting station stops it, so it never reaches the icing station or the box.
- The nut cookie is whole. Through it goes, gets its icing and lands in the box next to the jam one.
- 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.
- The stations are set up, but nobody has asked for anything yet. Not a single cookie moves.
- The taster arrives and asks for one iced cookie. Only now does the belt start.
- The first cookie, oat, is broken. The sorting station stops it before the icing station ever sees it.
- Next comes jam. It travels the whole belt, gets iced and reaches the taster, before any other cookie leaves the tray.
- The taster has their cookie, so the belt stops. The nut and choc cookies never leave the tray.
- The taster asks the same belt for another one. That belt run is over, so the answer is no. A new belt is needed.
- The packer's rule is one slot per flavour. A jam cookie arrives and goes in the jam slot.
- An oat cookie arrives and gets its own slot.
- A second jam cookie! The jam slot is already taken, and nobody told the packer what to do, so the whole belt stops.
- 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.
- On the ordinary belt, every weight card is put in its own little box before it can ride.
- At the end, someone unpacks every box before adding the weights up. Same total, lots of boxes.
- On the number belt the cards ride bare. The adder just presses the add-up button.
- With an empty tray, the add-up button says 0, but the average button says there's no average, rather than making one up.
- The cookies are split in two, and each half goes to a worker from the factory's shared crew.
- Both workers scribble on the wall notepad at the same moment. The notes get smudged and one is lost.
- The right way is to let the packer gather each worker's batch and put them back in the original order.
- 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.