Concise way to get both min and max value of Java 8 stream

前端 未结 6 1688
甜味超标
甜味超标 2020-12-15 03:53

Is there a concise way to extract both the min and max value of a stream (based on some comparator) in one pass?

There appear to be many ways to get the min and max

6条回答
  •  时光取名叫无心
    2020-12-15 04:28

    I think you need that

    IntStream myIntStream = IntStream.rangeClosed(1, 100);
    IntSummaryStatistics intStatistic = myIntStream.summaryStatistics();
    
    System.out.println("Max: " + intStatistic.getMax() + " Min: " + intStatistic.getMin());
    

提交回复
热议问题