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