import java.util.Collection; import java.util.Iterator; import java.util.ArrayList; import java.util.TreeSet; public class IteratorExample { public static Object max(Collection coll) { Iterator it = coll.iterator(); Object max = null; while (it.hasNext()) { Object cur = it.next(); if (max == null) { max = cur; } else if (((Comparable)max).compareTo(cur) < 0) { max = cur; } } return max; } public static Object max2(Collection coll) { Object max = null; for (Object cur : coll) { if (max == null) { max = cur; } else if (((Comparable)max).compareTo(cur) < 0) { max = cur; } } return max; } public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("fred"); list.add("wilma"); list.add("barney"); list.add("betty"); TreeSet set = new TreeSet(); set.add(new Integer(43)); set.add(new Integer(37)); set.add(new Integer(50)); set.add(new Integer(48)); System.out.println("max name is " + max2(list)); System.out.println("max temp is " + max2(set)); } }