Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a method that returns the maximum of a collection of comparable elements. Why does the JDK's Collections.max declare <T extends Object & Comparable<? super T>>?

A simple version is static <T extends Comparable<? super T>> T max(Collection<? extends T> items), which walks the elements and keeps the largest according to compareTo. The ? super T lets T use a compareTo inherited from a supertype, as java.sql.Timestamp does with Date's ordering. The JDK adds Object & in front of the bound so that the erasure of T is Object, which kept the method's pre-generics binary signature returning Object; multiple bounds are joined with &, with at most one class, which must come first.

Why this question is asked

Collections.max has one of the most famous signatures in the JDK, and every developer who reads it for the first time wonders whether it is a joke. It is not: each part solves a real problem, and together they show three separate ideas about generics. Interviewers ask this coding question because writing the method shows we can use bounded type parameters, and explaining the JDK's version shows we understand flexible bounds, erasure and binary compatibility. In this answer, we will recap bounded type parameters, write the method, explain why Comparable<? super T> is needed, explain the strange Object & part, and look at the rules for multiple bounds.

1A Quick Recap of Bounded Type Parameters

A type parameter can have an upper bound, written with extends, which limits the types a caller may use and tells the compiler what methods are available. In <T extends Comparable<T>>, T must be a type that can compare itself with another T, so inside the method we may call a.compareTo(b) on values of type T.

Comparable<T> is the interface with the single method int compareTo(T other), which returns a negative number, zero or a positive number when this is smaller, equal or larger. String implements Comparable<String>, Integer implements Comparable<Integer>, and so on.

2Writing max

Here is a straightforward version. It takes the first element as the best so far and replaces it whenever it finds a larger one.

Java
static <T extends Comparable<? super T>> T max(Collection<? extends T> items) {
Iterator<? extends T> it = items.iterator();
T best = it.next(); // NoSuchElementException if empty, like the JDK
while (it.hasNext()) {
T x = it.next();
if (x.compareTo(best) > 0) best = x;
}
return best;
}
max(List.of(3, 9, 4)); // 9
max(Set.of("pear", "apple", "fig")); // pear

The parameter is Collection<? extends T> because the method only reads from it, so a collection of any subtype of T works. Reading from an Iterator<? extends T> gives values we can safely store in a T. The interesting part is the bound on T, which is not simply Comparable<T>.

3Why Comparable<? super T>?

Consider java.sql.Timestamp. It extends java.util.Date, and Date implements Comparable<Date>. Timestamp does not implement Comparable<Timestamp>; it inherits Comparable<Date>. So a Timestamp is a Comparable<Date>, which can compare itself with any Date, including other Timestamps.

Java
static <T extends Comparable<T>> T strictMax(Collection<? extends T> items) { ... }
List<Timestamp> times = List.of(Timestamp.valueOf("2026-01-01 10:00:00"),
Timestamp.valueOf("2026-03-01 10:00:00"));
Timestamp t1 = strictMax(times); // error: inferred type Timestamp does not conform to bound Comparable<Timestamp>
Timestamp t2 = max(times); // 2026-03-01 10:00:00.0: Timestamp is a Comparable<Date>, and Date is a supertype of Timestamp

With the strict bound, T would have to be a Comparable<Timestamp>, and Timestamp is not. Notice that we assign the result to a Timestamp; without that target type, the compiler could quietly pick T as Date, which is a Comparable<Date>, and hand back a Date, so the strict version would only appear to work. With Comparable<? super T>, T only needs to be comparable to some supertype of itself, and comparing to Date, a supertype, is enough, because every Timestamp is a Date. This is the consumer-super rule: compareTo consumes a T, so the bound uses ? super. The same situation appears with any class that inherits compareTo from a parent, such as a subclass of a comparable domain class, or ScheduledFuture, which is comparable through its supertype Delayed.

4Why the JDK Writes Object & Comparable

The real JDK signature is public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll). The Object & part changes nothing for callers, since every type already extends Object. It exists because of erasure.

When the compiler erases a type parameter, it replaces it with its leftmost bound. With the bound Comparable<? super T> alone, T would erase to Comparable, so the compiled method would be Comparable max(Collection). But Collections.max existed before Java 5, compiled as Object max(Collection). Old class files compiled against Java 1.4 call a method with that exact descriptor, and the JVM links method calls by name and descriptor, including the return type. Changing the return type in the class file to Comparable would break every one of those old binaries with NoSuchMethodError. Putting Object first makes T erase to Object, so the method's binary signature stayed exactly what it was, while the source signature gained full generic checking.

What T erases to
DeclarationErasure of TCompiled signature
<T extends Comparable<? super T>>ComparableComparable max(Collection)
<T extends Object & Comparable<? super T>>ObjectObject max(Collection): same as Java 1.4
The leftmost bound decides the erasure, so Object & keeps old binaries linking.

5Rules for Multiple Bounds

A type parameter can have several bounds joined by &, which means T must satisfy all of them. At most one bound may be a class, and if there is a class it must come first; the rest must be interfaces. That mirrors Java's inheritance: a class can extend one class and implement many interfaces.

XML
<T extends Number & Comparable<? super T>> T clampedMax(...) // fine: class first, then interface
// <T extends Comparable<T> & Number> // error: the class must come first
// <T extends Number & Integer> // error: only one class bound
<T extends Runnable & AutoCloseable> void runAndClose(T task) { // two interfaces, fine
try (task) { task.run(); }
}

Inside the method we can use the methods of every bound, as runAndClose does with run() and close(). Multiple bounds are not common in application code, but they are the cleanest way to say a value must be two things at once, without inventing an interface that combines them.

6Follow-up questions

Why does the method take Collection<? extends T> rather than Collection<T>?

Because it only reads elements, so it can accept a collection of any subtype of T. That makes calls work when the inferred T is a supertype of the element type, which happens with the ? super T bound.

What happens if the collection is empty?

Both our version and the JDK's throw NoSuchElementException, because there is no first element to start from. When an empty input is normal, a stream with max(Comparator.naturalOrder()) returning Optional is a better fit.

Is there a version that takes a Comparator?

Yes: Collections.max(Collection<? extends T> coll, Comparator<? super T> comp). It has no bound on T at all, because the comparator does the comparing, and the comparator parameter uses ? super T for the same consumer reason.

7Conclusion

In this answer, we wrote max with <T extends Comparable<? super T>> and a Collection<? extends T> parameter. The ? super T lets types that inherit compareTo from a supertype, such as Timestamp comparing as a Date, be used. The JDK adds Object & so that T erases to Object, keeping the compiled signature Object max(Collection) that pre-generics binaries link against. Multiple bounds are joined with &, with at most one class, placed first.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write it as static <T extends Comparable<? super T>> T max(Collection<? extends T> items): get an iterator, take the first element as best, loop through the rest, and replace best whenever x.compareTo(best) is greater than zero.

Then, if they want more

The Collection<? extends T> is there because I only read from it. The bound Comparable<? super T> rather than Comparable<T> matters for classes that inherit compareTo from a supertype. java.sql.Timestamp extends Date, and Date implements Comparable<Date>, so Timestamp is a Comparable<Date>, not a Comparable<Timestamp>.

With the strict bound, max of a list of Timestamps would not compile; with ? super T it works, because comparing to a supertype is enough. The Object & in the JDK's version is about erasure and binary compatibility.

A type parameter erases to its leftmost bound, so without Object first, T would erase to Comparable and the compiled method would return Comparable. But Collections.max existed before generics as Object max(Collection), and old class files link to it by that exact descriptor, including the return type, so changing it would break them. Putting Object first keeps the erasure Object.

In general, multiple bounds use &, and at most one can be a class, which has to come first.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a method that returns the maximum of a collection of comparable elements. Why does...": https://scrivio-interview-library.netlify.app/java/generics/1115a7ceed/