FileDocCategorySizeDatePackage
Comparators.javaAPI DocExample2210Fri Nov 03 10:59:36 GMT 2006None

Comparators

public class Comparators extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        Comparator<String> sizeOrder = new Comparator<String> () {
            public int compare (String s1, String s2) {
                return
                    s1.length() < s2.length() ? -1 :
                    s1.length() > s2.length() ? 1 :
                    s1.compareTo(s2) ;
                }
        };
        Collection<String> strings = Arrays.asList("from","aaa","to","zzz");
        assert max(strings).equals("zzz");
        assert min(strings).equals("aaa");
        assert max(strings,sizeOrder).equals("from");
        assert min(strings,sizeOrder).equals("to");
    
public static Tmax(java.util.Collection coll, java.util.Comparator cmp)

        T candidate = coll.iterator().next();
        for (T elt : coll) {
            if (cmp.compare(candidate, elt) < 0) { candidate = elt; }
        }
        return candidate;
    
public static Tmax(java.util.Collection coll)

        return max(coll, Comparators.<T>naturalOrder());
    
public static Tmin(java.util.Collection coll, java.util.Comparator cmp)

        return max(coll, reverseOrder(cmp));
    
public static Tmin(java.util.Collection coll)

        return max(coll, Comparators.<T>reverseOrder());
    
public static java.util.ComparatornaturalOrder()

        return new Comparator<T> () {
            public int compare (T o1, T o2) { return o1.compareTo(o2); }
        };
    
public static java.util.ComparatorreverseOrder(java.util.Comparator cmp)

        return new Comparator<T> () {
            public int compare (T o1, T o2) { return cmp.compare(o2,o1); }
        };
    
public static java.util.ComparatorreverseOrder()

        return new Comparator<T> () { 
            public int compare (T o1, T o2) { return o2.compareTo(o1); }
        };