Ou Ye Ou Ye - 1 year ago 67
Java Question

How to write a generic findMax() method using collections.max()?

I am trying to create a generic method findMax(List list), that takes in either List of LocalDate or List of Date type, and return the maximum value in the list.

Collections.max(List LocalDate) and Collections.max(List Date) both works perfectly fine, but I don't know how to make it to return the correct type back.

Don't really understand how comparator works in Java.

Below is my attempt

static List<LocalDate> localDateList = new ArrayList<LocalDate>();
static List<Date> dateList = new ArrayList<Date>();

private <T> T findMax(List<T> list) {
return Collections.max(list);

public static void main(String[] args) throws ParseException, SQLException, JsonProcessingException {

localDateList.add(new Date(11 * 86400000).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
localDateList.add(new Date(22 * 86400000).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
localDateList.add(new Date(3 * 86400000).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
localDateList.add(new Date(14 * 86400000).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
localDateList.add(new Date(65 * 86400000).toInstant().atZone(ZoneId.systemDefault()).toLocalDate());

dateList.add(new Date(11 * 86400000));
dateList.add(new Date(22 * 86400000));
dateList.add(new Date(3 * 86400000));
dateList.add(new Date(14 * 86400000));
dateList.add(new Date(65 * 86400000));




Got it to work by changing from

private <T> T findMax(List<T> list) {
return Collections.max(list);


private <T extends Object & Comparable<? super T>> T findMax(List<T> list) {
return Collections.max(list);

Answer Source


No need to write your findMax method. The Collections.max already does that for you.

List<LocalDate> localDates = new ArrayList<> ( 3 );
LocalDate today = LocalDate.now ( ZoneId.of ( "America/Montreal" ) );
localDates.add ( today );
localDates.add ( today.plusDays ( 2 ) );
localDates.add ( today.minusDays ( 4 ) );

LocalDate max = Collections.max ( localDates );

Dump to console.

System.out.println ( "localDates: " + localDates + " | max: " + max );

localDates: [2016-08-18, 2016-08-20, 2016-08-14] | max: 2016-08-20


Any class implementing the Comparable interface must implement a compareTo method. That method must return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than another object of the same type.

The LocalDate class has this compareTo method implemented.

The Collections.max method loops the elements of a collection such as a List and calls each object’s compareTo method. All that is done on your behalf, to relieve you of the chore of writing such code.


Comments suggest the Question was really more about Java Generics that about comparing.

Here is an example of implementing your findMax method. I followed the example of the Collections.max source code for Java 8 in the OpenJDK project.

As I said above, there is no productive use for merely wrapping the call to Collections.max. But it does make a nice experimental example of using generics.

public <T extends Object & Comparable<? super T>> T findMax ( Collection<? extends T> coll ) {
    T maximum = Collections.max ( coll );
    return maximum;

Calling that method and passing the localDates collection seen above.

System.out.println ( "findMax: " + this.findMax ( localDates ) );