Using Java 8 stream methods to get a max value

前端 未结 5 1249
暖寄归人
暖寄归人 2021-01-22 00:00

I would like to get the max value out of a list using java 8 stream methods.

The structure is the following:

  • I read a csv file and store the data of every
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 00:42

    using reduce

    int max1 = arrRound.stream()
            .flatMap(r -> r.getHits().stream())
            .mapToInt(h -> h.getPrizeAmount())
            .reduce(Math::max) //returns OptionalInt
            .orElse(Integer.MIN_VALUE);
    

    or

    int max2 = arrRound.stream()
            .flatMap(r -> r.getHits().stream())
            .mapToInt(h -> h.getPrizeAmount())
            .reduce(Integer.MIN_VALUE, Math::max);
    

提交回复
热议问题