How to calculate mean, median, mode and range from a set of numbers

后端 未结 7 1544
闹比i
闹比i 2020-12-07 13:34

Are there any functions (as part of a math library) which will calculate mean, median, mode and range from a set of numbers.

相关标签:
7条回答
  • 2020-12-07 14:02

    If you only care about unimodal distributions, consider sth. like this.

    public static Optional<Integer> mode(Stream<Integer> stream) {
        Map<Integer, Long> frequencies = stream
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
        return frequencies.entrySet().stream()
            .max(Comparator.comparingLong(Map.Entry::getValue))
            .map(Map.Entry::getKey);
    }
    
    0 讨论(0)
提交回复
热议问题