I have a stream of records and I need to find all the records with minimum value of field.
Lets say I have following Person record
record Person(String name, BigDecimal loanAmount, LocalDate dateOfBirth) {}
.
I need to find all the persons with minimum loanAmount. I found two ways to do this
- I can find the minimum loanAmount using min operator and then iterate over the list once more to filter and collect all the Persons with min loanAmount
- I can use Collectors.groupingBy to create a
Map<BigDecimal(loanAmount), List<Person>>
and then get the List from Map with min loanAmount.
I am wondering if I could collect only the Persons with minimum loanAmount with just single pass without having to collect other Person in map which I dont need.
Note: I am getting the Persons through a web API as json and then I am converting those to java records and then I am doing all this processing so I am wondering if there is an elegant way of doing this in one pass.
Thank you for reading and answering the question in advance.