Java tutorial
//package com.java2s; import java.util.*; public class Main { public static <E> Optional<E> max(List<E> elements, Comparator<E> comparator) { if (elements.size() != 0) { E maxElement = elements.get(0); for (int i = 1; i < elements.size(); i++) { maxElement = comparator.compare(maxElement, elements.get(i)) > 0 ? maxElement : elements.get(i); } return Optional.of(maxElement); } else return Optional.empty(); } }