Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Collections;
import java.util.Comparator;

import java.util.List;

public class Main {
    /**
     * 
     * @param <T>
     * @param a
     * @return
     */
    public static <T extends Comparable<T>> T getLowerMiddleValue(T... a) {
        if (null == a || a.length <= 0)
            return null;
        List<T> list = new ArrayList<T>();
        for (T t : a) {
            if (null != t) {
                list.add(t);
            }
        }
        return getLowerMiddleValue(list);
    }

    /**
     * 
     * @param <T>
     * @param list
     * @return
     */
    public static <T extends Comparable<T>> T getLowerMiddleValue(List<T> list) {
        if (null == list || list.isEmpty())
            return null;
        Collections.sort(list);
        return list.get((list.size() - 1) / 2);
    }

    /**
     * 
     * @param <T>
     * @param comparator
     * @param a
     * @return
     */
    public static <T> T getLowerMiddleValue(Comparator<T> comparator, T... a) {
        if (null == a || a.length <= 0)
            return null;
        Arrays.sort(a, comparator);
        return a[(a.length - 1) / 2];
    }

    /**
     * 
     * @param <T>
     * @param list
     * @param comparator
     * @return
     */
    public static <T> T getLowerMiddleValue(List<T> list, Comparator<T> comparator) {
        if (null == list || list.isEmpty())
            return null;
        Collections.sort(list, comparator);
        return list.get((list.size() - 1) / 2);
    }
}