Java List Median getMedian(Collection list)

Here you can find the source of getMedian(Collection list)

Description

Find the median of a Collection of Doubles.

License

GNU General Public License

Parameter

Parameter Description
list The collection of doubles.

Return

The median value.

Declaration

public static double getMedian(Collection<Double> list) 

Method Source Code

//package com.java2s;
/*//from w w w  . ja va 2s  . c o m
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.util.*;

public class Main {
    /**
     * Find the median of a Collection of Doubles.
     * For an odd number of values, returns the middle value.
     * For an even number, returns the average of the middle two values.
     * @param list The collection of doubles.
     * @return The median value.
     */
    public static double getMedian(Collection<Double> list) {
        ArrayList<Double> sortList = new ArrayList<Double>(list);
        Collections.sort(sortList);
        int size = list.size();
        double v1 = ((Double) sortList.get((size - 1) / 2)).doubleValue();
        double v2 = ((Double) sortList.get(size / 2)).doubleValue();
        return (v1 + v2) / 2;
    }
}

Related

  1. calcMedian(List values, int start, int end)
  2. calculateMAD(List values, Number median)
  3. calculateMedian(List values)
  4. calculateMedian(List values, boolean copyAndSort)
  5. getGeneMedian(final List doubleArray)
  6. getMedian(List list)
  7. getMedian(List numbers)
  8. getMedian(List results)
  9. getMedianValue(List nums)