I have a list of locations returned by Google places API and decided to find a Place with a lowest price.
Here is how I implemented it with Java 8:
BigDecimal lowestPrice = places.stream()
.collect(Collectors.groupingBy(Place::getPrice, Collectors.counting()))
.entrySet().stream().min(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(BigDecimal.ZERO);
It returns me the lowest price but it'll be great to get a name of the Place as well (it has a name attribute).
How can I also return a name of the place with a lowest price?
places.stream().min(Comparator.comparing(Place::getPrice)).get();