← All Exercises

Java Generic Methods

intermediatejava~20min

Background

Java generics with bounded type parameters enable writing type-safe algorithms that work across related types.

Problem

Create a generic Java method that finds the maximum element in a list using a bounded type parameter extending Comparable.

Input

A list of Comparable elements.

Output

An Optional containing the maximum element, or empty for an empty list.

Sample tests

Sample 1

Input: [3, 7, 2, 9, 1]
Expected: Optional[9]

Your solution

Learning objective

Learn bounded type parameters, Comparable interface, and generic method declaration syntax.