Explain Java code

Paste Java, from a record to a stream pipeline, and Vibe explains what each step does and which types it works with.

Reports.java

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe23 lines selected

Press Explain it for the sample, or paste your own code first.

What this Java sample shows

classes
record Order(...) declares a class from its three fields. Java writes the constructor, the accessors customer(), status() and cents(), and equals, hashCode and toString. The block inside is a compact constructor: it runs on every new Order(...) and throws IllegalArgumentException for a negative total.
streams
paidByCustomer reads the list as a stream. filter keeps the paid orders, then Collectors.groupingBy puts them in a TreeMap keyed by customer and adds up their cents with summingInt. Paid orders of 1250 and 2750 for Ana and 3000 for Ben give {Ana=4000, Ben=3000}.
generics
topKey works for any map whose values can be compared. The bound V extends Comparable<V> is what lets Map.Entry.comparingByValue() compile: with a plain V, javac rejects the method with an incompatible types error.

Good to know

  • A customer with no paid orders is left out of the result, not listed with 0. A customer whose only order is PENDING never reaches groupingBy.
  • TreeMap::new keeps the customers in name order. Without it, groupingBy promises no particular map or order; the JDK hands back a HashMap.
  • topKey returns an Optional because an empty map has no top key: it gives Optional.empty, never null. The spec doesn't say which entry wins a tie; the JDK keeps the one max met first, which in a TreeMap is the name that sorts first.
  • o.status().equals("PAID") throws NullPointerException on an order whose status is null. Written as "PAID".equals(o.status()), it skips that order instead.

Watch out: summingInt adds in an int, so a customer's total silently wraps around once it passes 2,147,483,647 cents, about 21.5 million dollars. Two paid orders of 2,000,000,000 cents add up to -294967296. Use summingLong for totals that can grow that large.

Explain Java code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).