Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Given a DoubleStream s, I can do s.min() or s.max() but not both, as any one of them will consume the stream.

Now suppose I have

class Range /* can add code here */ {
    private final double min;
    private final double max;
    Range(double min, double max){
        this.min = min;
        this.max = max;
    }
    // can add code here
}

How can I get the range of the stream? (Other than by s.collect(Collectors.toList()); new Range(s.stream().min(),s.stream().max());)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
108 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can call DoubleStream's summaryStatistics method. It returns a DoubleSummaryStatistics object that contains a min and a max, plus a few other statistics: average, count, and sum.

IntStream and LongStream have similar summaryStatistics methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...