Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Find max from a list of structures in Java without using streams
static class Employee {
String firstName;
String surname;
double salary; //yeah, right!
public Optional<Employee> max(Collection<Employee> collection) {
if (collection == null || collection.isEmpty()) return Optional.empty();
Iterator<Employee> employeeIterator = collection.iterator();
Employee max =;
while (employeeIterator.hasNext()) {
Employee n =;
if (n.salary > max.salary) max = n;
return Optional.of(max);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment