Created
April 1, 2017 00:20
-
-
Save liviutudor/1e588f582b687a1bb851d4e120df98fc to your computer and use it in GitHub Desktop.
Find max from a list of structures in Java without using streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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