get the lowest value from a List. - Java java.util

Java examples for java.util:List Element

Description

get the lowest value from a List.

Demo Code


//package com.book2s;

import java.util.ArrayList;

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

import java.util.List;

public class Main {
    /**/* w w w .ja v  a2s. co m*/
     * 
     * @param <T>
     * @param a
     *            variable number of arguments of type <T>
     * @return
     */
    public static <T extends Comparable<T>> T lowestValue(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);
            }
        }
        Collections.sort(list);
        return list.get(0);
    }

    /**
     * This API is to get the lowest value from a List. The input objects need
     * to implement Comparable.
     * 
     * 
     * @param <T>
     *            The type of the object the input list can contain and also the
     *            return type of the method
     * @param list
     * @return
     * @author sabuj.das
     */
    public static <T extends Comparable<T>> T lowestValue(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        Collections.sort(list);
        return list.get(0);
    }

    /**
     * 
     * @param <T>
     * @param list
     * @param comparator
     * @return
     * @author sabuj.das
     */
    public static <T> T lowestValue(List<T> list, Comparator<T> comparator) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        Collections.sort(list, comparator);
        return list.get(0);
    }
}

Related Tutorials