Here you can find the source of getMedian(Collection
Parameter | Description |
---|---|
list | The collection of doubles. |
public static double getMedian(Collection<Double> list)
//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; } }