Skip to content

Instantly share code, notes, and snippets.

@liviutudor
Created April 1, 2017 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liviutudor/1e588f582b687a1bb851d4e120df98fc to your computer and use it in GitHub Desktop.
Save liviutudor/1e588f582b687a1bb851d4e120df98fc to your computer and use it in GitHub Desktop.
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 = employeeIterator.next();
while (employeeIterator.hasNext()) {
Employee n = employeeIterator.next();
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