Java List Median calculateMedian(List values)

Here you can find the source of calculateMedian(List values)

Description

Utility method to calculate the median of a given list of values.

License

Apache License

Parameter

Parameter Description
values the double values for which to calculate the median.

Exception

Parameter Description
IllegalStateException if the provided argument is null or empty.

Return

the median of the provided values

Declaration

public static double calculateMedian(List<Double> values) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class Main {
    /**//www  .  ja va  2s. c o m
     * Utility method to calculate the median of a given list of values. Note:
     * this will throw an IllegalStateException if the provided argument is null
     * or empty.
     *
     * @param values the double values for which to calculate the median.
     * @return the median of the provided values
     * @throws IllegalStateException if the provided argument is null or empty.
     */
    public static double calculateMedian(List<Double> values) {
        if (values == null || values.isEmpty()) {
            throw new IllegalStateException("Can not calculate median on empty list or null!");
        }

        // if there is only one value, then it automatically is the median
        if (values.size() == 1) {
            return values.get(0);
        }

        double[] valueArray = toArray(values);
        return calculateMedian(valueArray);
    }

    public static double calculateMedian(double[] values) {
        if (values == null || values.length == 0) {
            throw new IllegalArgumentException("Can not calculate median of null or empty array!");
        }

        // if there is only one value, then it automatically is the median
        if (values.length == 1) {
            return values[0];
        }

        double result; // the median we are going to calculate
        Arrays.sort(values);

        int middle = values.length / 2; // determine the middle of the list
        if (values.length % 2 == 0) { // even number of elements (middle is between two values)
            // build the average between the two values next to the middle
            result = (values[middle] + values[middle - 1]) / 2d;
        } else { // uneven number of elements (middle is one value of the list)
            result = values[middle];
        }

        return result;
    }

    public static double[] toArray(Collection<Double> values) {
        if (values == null) {
            throw new IllegalArgumentException("No null value allowed!");
        }
        double[] result = new double[values.size()];
        int cnt = 0;
        for (Double value : values) {
            result[cnt++] = value;
        }
        return result;
    }
}

Related

  1. calcMedian(List values, int start, int end)
  2. calculateMAD(List values, Number median)
  3. calculateMedian(List values, boolean copyAndSort)
  4. getGeneMedian(final List doubleArray)
  5. getMedian(Collection list)
  6. getMedian(List list)