Java List Median getMedian(List numbers)

Here you can find the source of getMedian(List numbers)

Description

Given a list of numbers returns the median of the of the list

License

Open Source License

Parameter

Parameter Description
numbers List<Double> containing the list of numbers

Return

Double containing the median of the list of numbers

Declaration

private static Double getMedian(List<Double> numbers) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collections;
import java.util.List;

public class Main {
    /** /*from  w  ww .  j  a  va  2  s.c  o m*/
     * Given a list of numbers returns the median of the of the list
     * @param numbers List<Double> containing the list of numbers
     * @return Double containing the median of the list of numbers
     */
    private static Double getMedian(List<Double> numbers) {

        double median;
        Collections.sort(numbers);
        int size = numbers.size();

        if (size % 2 != 0) {
            median = numbers.get(size / 2);
        } else {
            if (size != 1) {
                median = (numbers.get(size / 2) + numbers.get((size / 2) - 1)) / 2.0;
            } else {
                median = numbers.get(size / 2);
            }
        }

        return median;
    }
}

Related

  1. calculateMedian(List values)
  2. calculateMedian(List values, boolean copyAndSort)
  3. getGeneMedian(final List doubleArray)
  4. getMedian(Collection list)
  5. getMedian(List list)
  6. getMedian(List results)
  7. getMedianValue(List nums)
  8. median(ArrayList values)
  9. median(final List array)