Java tutorial
//package com.java2s; import java.util.Comparator; public class Main { public static <T extends Comparable<T>> T maxElement(Iterable<T> collection) { T result = null; for (T element : collection) { if (result == null || result.compareTo(element) < 0) result = element; } return result; } public static <T> T maxElement(Iterable<T> collection, Comparator<T> comparator) { T result = null; for (T element : collection) { if (result == null || comparator.compare(result, element) < 0) result = element; } return result; } }