首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统计计算

统计计算
EN

Stack Overflow用户
提问于 2010-08-20 21:42:35
回答 5查看 1.9K关注 0票数 4

在java中有没有内置的库可以用来计算中位数??

我正在使用apache.commons.math进行其他统计函数的工作,但找不到median。

谢谢,

EN

Stack Overflow用户

发布于 2010-08-20 22:35:04

来自“我手上的时间太多”部门:这是一个小的MedianGenerator类:

代码语言:javascript
运行
复制
/**
 * Methods to calculate the median value of a supplied {@link List}.
 */
public final class MedianGenerator{

    private MedianGenerator(){
    }

    /**
     * Calculate the median of a supplied list.
     * <ol>
     * <li>A copy will be generated</li>
     * <li>this copy will be sorted with the supplied comparator</li>
     * <li>the median will be calculated, using the supplied averageCalculator
     * for collections with an even number of items</li>
     * </ol>
     * 
     * @param data 
     * @param comparator
     * @param averageCalculator
     * @return the median
     */
    public static <T> T calculateMedian(final List<T> data,
        final Comparator<? super T> comparator,
        final AverageCalculator<T> averageCalculator){
        final List<T> copy = new ArrayList<T>(data);
        Collections.sort(copy, comparator);
        return doCalculateMedian(data, averageCalculator);

    }

    /**
     * Calculate the median of a supplied list.
     * <ol>
     * <li>A copy will be generated</li>
     * <li>this copy will be sorted with the supplied comparator</li>
     * <li>the median will be calculated, using the {@link #ALWAYS_FIRST} {@link AverageCalculator}
     * for collections with an even number of items</li>
     * </ol>
     * 
     * @param data 
     * @param comparator
     * @return the median
     */
    @SuppressWarnings("unchecked")
    public static <T> T calculateMedian(final List<T> data,
        final Comparator<? super T> comparator){
        return calculateMedian(data, comparator, (AverageCalculator<T>) ALWAYS_FIRST);
    }

    /**
     * Calculate the median of a supplied list.
     * <ol>
     * <li>A copy will be generated</li>
     * <li>this copy will be sorted using natural ordering</li>
     * <li>the median will be calculated, using the {@link #ALWAYS_FIRST} {@link AverageCalculator}
     * for collections with an even number of items</li>
     * </ol>
     * 
     * @param data 
     * @return the median
     */
    @SuppressWarnings("unchecked")
    public static <T extends Comparable<? super T>> T calculateMedian(final List<T> data){
        return calculateMedian(data, (AverageCalculator<T>) ALWAYS_FIRST);
    }

    /**
     * Calculate the median of a supplied list.
     * <ol>
     * <li>A copy will be generated</li>
     * <li>this copy will be sorted using natural ordering</li>
     * <li>the median will be calculated, using the supplied averageCalculator
     * for collections with an even number of items</li>
     * </ol>
     * 
     * @param data
     * @param averageCalculator 
     * @return the median
     */
    public static <T extends Comparable<? super T>> T calculateMedian(final List<T> data,
        final AverageCalculator<T> averageCalculator){
        final List<T> copy = new ArrayList<T>(data);
        Collections.sort(copy);
        return doCalculateMedian(copy, averageCalculator);
    }

    private static <T> T doCalculateMedian(final List<T> sortedData,
        final AverageCalculator<T> averageCalculator){
        T result;
        if(sortedData.isEmpty()){
            result = null;
        } else{
            final int size = sortedData.size();
            if(size % 2 == 0){
                result =
                    averageCalculator.getAverage(sortedData.get(size / 2 - 1),
                        sortedData.get(size / 2));
            } else{
                result = sortedData.get(size / 2 - 1);
            }

        }
        return result;
    }

    /**
     * Generic accessor method for {@link #ALWAYS_FIRST}.
     */
    @SuppressWarnings("unchecked")
    public static <T> AverageCalculator<T> alwaysFirst(){
        return ALWAYS_FIRST;
    }

    /**
     * {@link AverageCalculator} implementation that always returns the lower
     * bound unchanged.
     */
    @SuppressWarnings("rawtypes")
    public static final AverageCalculator ALWAYS_FIRST =
        new AverageCalculator(){

            @Override
            public Object getAverage(final Object first, final Object second){
                return first;
            }

        };

    /**
     * When there is an even number of items, this interface is used to generate
     * the average between the two middle items.
     */
    public static interface AverageCalculator<E> {

        E getAverage(E first, E second);
    }

}
票数 1
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3531374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档