max value from Collection - Java java.util

Java examples for java.util:Collection Algorithm

Description

max value from Collection

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) throws Exception {
        Collection list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(max(list));
    }/*  w w w. ja  va2  s  .c o  m*/

    public static Number max(Collection<Number> list) {
        Number max = list.iterator().next();
        for (Number item : list) {
            if (item.floatValue() > max.floatValue()) {
                max = item;
            }
        }
        return max;
    }
}

Related Tutorials