Java List Median median(List list)

Here you can find the source of median(List list)

Description

This method returns you the median value for a List of Doubles

License

BSD License

Parameter

Parameter Description
list a parameter

Declaration

public static Double median(List<Double> list) 

Method Source Code


//package com.java2s;
/*L//from   w  w w .  ja v a  2 s  . c o m
 *  Copyright SAIC
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/stats-analysis/LICENSE.txt for details.
 */

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

public class Main {
    /**
     * This method returns you the median value for a List of Doubles
     * @param list
     * @return
     */
    public static Double median(List<Double> list) {
        //     List must be first sorted
        Collections.sort(list);
        int middle = list.size() / 2; // subscript of middle element
        if (list.size() % 2 == 1) {
            // Odd number of elements -- return the middle one.
            return list.get(middle);
        } else {
            // Even number -- return average of middle two
            // Must cast the numbers to double before dividing.
            return (list.get(middle - 1) + list.get(middle)) / 2.0;
        }
    }
}

Related

  1. getMedian(List results)
  2. getMedianValue(List nums)
  3. median(ArrayList values)
  4. median(final List array)
  5. median(List data)
  6. median(List list)
  7. median(List p)
  8. median(List sortedDataAscendingOrder)
  9. median(List list)