Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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();
    }
}