Generic methods: Max. Min : Generic Method « Generics « Java Tutorial






import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

class Comparators {
  public static <T> T max(Collection<? extends T> coll, Comparator<? super T> cmp) {
    T candidate = coll.iterator().next();
    for (T elt : coll) {
      if (cmp.compare(candidate, elt) < 0) {
        candidate = elt;
      }
    }
    return candidate;
  }

  public static <T extends Comparable<? super T>> T max(Collection<? extends T> coll) {
    return max(coll, Comparators.<T> naturalOrder());
  }

  public static <T> T min(Collection<? extends T> coll, Comparator<? super T> cmp) {
    return max(coll, reverseOrder(cmp));
  }

  public static <T extends Comparable<? super T>> T min(Collection<? extends T> coll) {
    return max(coll, Comparators.<T> reverseOrder());
  }

  public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
    return new Comparator<T>() {
      public int compare(T o1, T o2) {
        return o1.compareTo(o2);
      }
    };
  }

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

  public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
    return new Comparator<T>() {
      public int compare(T o1, T o2) {
        return o2.compareTo(o1);
      }
    };
  }


}

public class MainClass{
  public static void main(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("AAA", "aaa", "CCC", "f");
    
    System.out.println(Comparators.max(strings));
    System.out.println(Comparators.min(strings));
    System.out.println(Comparators.max(strings,sizeOrder));
    System.out.println(Comparators.min(strings,sizeOrder));
  }
}
f
AAA
aaa
f








12.3.Generic Method
12.3.1.Creating a Generic Method
12.3.2.Generic Constructors
12.3.3.Generic methods: Max. Min
12.3.4.Generic method maximum returns the largest of three objects
12.3.5.Using generic methods to print array of different types
12.3.6.Wildcard test program