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.
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 accessorscustomer(),status()andcents(), andequals,hashCodeandtoString. The block inside is a compact constructor: it runs on everynew Order(...)and throwsIllegalArgumentExceptionfor a negative total.- streams
paidByCustomerreads the list as a stream.filterkeeps the paid orders, thenCollectors.groupingByputs them in aTreeMapkeyed by customer and adds up theircentswithsummingInt. Paid orders of 1250 and 2750 for Ana and 3000 for Ben give{Ana=4000, Ben=3000}.- generics
topKeyworks for any map whose values can be compared. The boundV extends Comparable<V>is what letsMap.Entry.comparingByValue()compile: with a plainV, javac rejects the method with anincompatible typeserror.
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
PENDINGnever reachesgroupingBy. TreeMap::newkeeps the customers in name order. Without it,groupingBypromises no particular map or order; the JDK hands back aHashMap.topKeyreturns anOptionalbecause an empty map has no top key: it givesOptional.empty, nevernull. The spec doesn't say which entry wins a tie; the JDK keeps the onemaxmet first, which in aTreeMapis the name that sorts first.o.status().equals("PAID")throwsNullPointerExceptionon an order whose status isnull. 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).