Library › Coding AI › Software developers
Convert a Java loop to a stream without losing key order
Practice: Setting limits and rules. About 5 minutes.
About this exercise
Convert idsByCustomer to a single stream pipeline. The invoice printer relies on customers appearing in first-seen order, so the result must stay a LinkedHashMap. Also, from now on customer ids must be trimmed and upper-cased (the new import has values like ' acme '). Skip VOID orders as before, and now also skip test orders, which have cents of 0. Keep the signature. Return only the method in exactly 1 code block of at most 10 lines, with under 15 words of text.
Common mistakes when you ask AI for this
- Losing the order. A plain groupingBy returns a HashMap, so the invoice index comes out shuffled. Try: Add: "Keep first-seen order: groupingBy(key, LinkedHashMap::new, mapping(...))."
- Not mentioning the new rule. The AI only converts what's there; it can't know ids must now be cleaned. Try: Add: "Group by customerId().trim().toUpperCase()."
- Missing the test-order filter. The AI only keeps the VOID check it can see; it can't know 0-cent test orders must go. Try: Add: "Also skip orders with cents == 0. Keep the signature; one return statement."
- The whole class and a lecture. The AI reprints the class and explains streams. Try: Add: "Only the method, one code block, no text."
What a strong prompt includes
- Protects the order
- Gives the new id rule
- Adds the test-order filter
- Protects the signature
- Asks for only the method
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)