Quick revision
The short answer
Declare record OrderTotal(Order order, BigDecimal total) {} inside the method, map each order to an OrderTotal whose total is the sum of its line amounts, sort by OrderTotal::total, and map back to the order. The local record gives the intermediate result named fields instead of a Map.Entry or an array, and it computes each total once. Local records are implicitly static, so they cannot capture local variables or the enclosing instance.
What to remember
In this answer, we sorted orders by a computed total with a local record OrderTotal(Order order, BigDecimal total) declared inside the method. Each total is computed once with reduce, the stream sorts by the named total component and maps back to the order, and no new top-level class is added. We saw why named fields beat Map.Entry or arrays, and that local records are implicitly static, so they cannot capture local variables or the enclosing instance; local enums and interfaces were allowed in the same Java 16 release.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Declare record OrderTotal(Order order, BigDecimal total) {} inside the method, map each order to an OrderTotal whose total is the sum of its line amounts, sort by OrderTotal::total, and map back to the order. The local record gives the intermediate result named fields instead of a Map.Entry or an array, and it computes each total once. Local records are implicitly static, so they cannot capture local variables or the enclosing instance.
Why this question is asked
Sorting by a computed value is a very common task: orders by total, customers by lifetime spend, files by size of their contents. Before Java 16, we either recomputed the value inside the comparator, wrapped it in an awkward Map.Entry, or created a top-level class just for this one method. Interviewers use this small coding question to check that we know local records and use them well. In this answer, we will set up the data, look at the first attempts and their problems, write the local record solution, walk through what each line does, and look at the rules that apply to local records.
1Setting Up the Data
We have orders, each with a list of lines, and each line has an amount. The total of an order is not stored; we compute it by adding up its line amounts with BigDecimal, which we use for money because it represents decimal values exactly.
2First Attempts and Their Problems
The most direct attempt computes the total inside the comparator. It works, but sorting compares each order many times, about n log n comparisons in total, and every comparison sums both orders' lines again. For a few orders that does not matter; for ten thousand orders with many lines it is wasted work, and the comparator is hard to read.
The second attempt computes each total once by pairing the order with its total in a Map.Entry. It is correct, but getKey() and getValue() say nothing about what they hold, and the next intermediate value we need would make it worse. A dedicated top-level class OrderTotal would fix the names but adds a public type to the codebase for one method.
3The Local Record Solution
A local record is a record declared inside a method body. It is visible only inside that method, so it adds a named type exactly where it is needed and nowhere else.
- Order A with two linesstart of the pipeline
- map: new OrderTotal(A, 45.00)lines summed once with reduce
- sorted by OrderTotal::totalB 12.50, C 25.00, A 45.00
- map: OrderTotal::orderunwrap back to the order
- toList()[B, C, A], unmodifiable
5 steps in order. Play them, or step through them one at a time.
The total is computed once per order and carried in a named field.
Let us read it line by line. The map step turns each Order into an OrderTotal, computing the total once: map(Line::amount) takes each line's amount, and reduce(BigDecimal.ZERO, BigDecimal::add) adds them up starting from zero, giving 45.00, 12.50 and 25.00. The sorted step compares OrderTotal objects by their total component, using BigDecimal's natural order, so it never recomputes anything. The last map unwraps each OrderTotal back to its Order, and toList() collects the result into an unmodifiable list, available since Java 16. For the largest totals first, we would write Comparator.comparing(OrderTotal::total).reversed().
4Rules for Local Records
Local records are implicitly static. That means they have no hidden reference to the enclosing object and cannot capture the method's local variables, unlike local classes, which can capture effectively final locals. The compiler rejects any attempt to do so.
The reason is that a record's state should be exactly its components. A captured variable would be hidden state that equals, hashCode and toString know nothing about. In practice this restriction rarely hurts, because the record is used inside the method where the values are already in scope. The same Java 16 release, through JEP 395, also allowed local enums and local interfaces, which are implicitly static for the same reason.
5Follow-up questions
Is creating an extra object per order a performance concern?
Rarely. The OrderTotal objects are small and short-lived, which is the cheapest kind of allocation for modern garbage collectors, and the JIT can sometimes remove them altogether. Avoiding the repeated summing in the comparator saves far more than the allocation costs.
Could we use a record as the return type of the method?
Not a local one, because it is not visible outside the method. If callers need the totals as well, promote OrderTotal to a nested or top-level record and return List<OrderTotal>.
Can a local record implement an interface or have methods?
Yes. A local record can implement interfaces such as Comparable, declare methods, static methods and a compact constructor, just like a top-level record. It only cannot capture locals or the enclosing instance.
6Conclusion
In this answer, we sorted orders by a computed total with a local record OrderTotal(Order order, BigDecimal total) declared inside the method. Each total is computed once with reduce, the stream sorts by the named total component and maps back to the order, and no new top-level class is added. We saw why named fields beat Map.Entry or arrays, and that local records are implicitly static, so they cannot capture local variables or the enclosing instance; local enums and interfaces were allowed in the same Java 16 release.
Open with
I would declare a local record inside the method, record OrderTotal with an Order and a BigDecimal total. Then the stream maps each order to new OrderTotal(o, total), where the total is o.lines().stream().map(Line::amount).reduce(BigDecimal.ZERO, BigDecimal::add).
Then, if they want more
After that I sort with Comparator.comparing(OrderTotal::total), map back with OrderTotal::order and call toList(). The point of the record is twofold. First, each total is computed once, instead of being recomputed inside the comparator on every comparison, which would be about n log n sums.
Second, the intermediate value has real names, order and total, instead of getKey and getValue on a Map.Entry or positions in an Object array, and I do not add a top-level class just for one method, because the local record is visible only inside it. For descending order I would add reversed() to the comparator.
Something to know about local records is that they are implicitly static, so they cannot use the method's local variables or the enclosing instance's fields. If I need a limit inside the record, I pass it in as a parameter or a component. That is deliberate, because a record's state is supposed to be exactly its components. Java 16, with the same JEP, also allowed local enums and local interfaces.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Use a local record to sort orders by total value computed from line items, without...": https://scrivio-interview-library.netlify.app/java/records/030a665edd/