Java examples for java.util:Collection Element
Finds the maximum element in the collection
//package com.book2s; import java.util.Collection; import java.util.Iterator; public class Main { public static void main(String[] argv) { Collection c = java.util.Arrays.asList("asdf", "book2s.com"); System.out.println(getMax(c)); }/* www .j a v a 2 s . c o m*/ /** * Finds the maximum element in the collection * @param c the collection we are searching in * @return the max element in collection c */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <G> G getMax(Collection<G> c) { if (c.isEmpty() == true) { throw new UnsupportedOperationException( "Can't find min in empty collection"); } Iterator<G> itr = c.iterator(); G max = itr.next(); while (itr.hasNext()) { G current = itr.next(); if (((Comparable) max).compareTo((Comparable) current) < 0) { max = current; } } return max; } }