Java List Median median(List data)

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

Description

median

License

Open Source License

Declaration

public static double median(List<? extends Number> data) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Gabriel Skantze.//from   ww w.  j  a  v a 2s  .  c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Gabriel Skantze - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    public static double median(List<? extends Number> data) {
        ArrayList<Double> sorted = new ArrayList<Double>(data.size());
        for (int i = 0; i < data.size(); i++) {
            sorted.add(data.get(i).doubleValue());
        }
        Collections.sort(sorted);
        if (sorted.size() % 2 == 1) {
            return sorted.get(sorted.size() / 2);
        } else {
            int mid = sorted.size() / 2 - 1;
            return (sorted.get(mid) + sorted.get(mid + 1)) / 2;
        }
    }
}

Related

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