I would like to get the max value out of a list using java 8 stream methods.
The structure is the following:
You can achieve it by this way :
Round
of the list
Round
object to its List hits
,flatMap
to go from Stream>
to Stream
Hits
to its prizeAmount
fieldmax
if exists-1
So a solution using Method reference
int maxPrize = arrRoundarrRound.stream() // Stream
.map(Round::getHits) // Stream>
.flatMap(List::stream) // Stream
.mapToInt(Hit::getPrizeAmount) // IntStream
.max() // OptionalInt
.orElse(-1); // int
With class lambda
and map + flatMap
in one :
int maxPrize = arrRoundarrRound.stream()
.flatMap(round -> round.getHits().stream())
.mapToInt(hits -> hits.getPrizeAmount())
.max()
.orElse(-1);