Find by name, extract salary and compute max in java, no 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
public Optional<Double> max(Collection<Double> collection) { | |
if (collection == null || collection.isEmpty()) return Optional.empty(); | |
Iterator<Double> iter = collection.iterator(); | |
Double max = iter.next(); | |
while (iter.hasNext()) { | |
Double n = iter.next(); | |
if (n > max) max = n; | |
} | |
return Optional.of(max); | |
} | |
public Collection<Employee> findName(Collection<Employee> collection, String name) { | |
List<Employee> list = new ArrayList<>(); | |
if (collection == null || collection.isEmpty()) return list; | |
for (Employee e : collection) { | |
if (e.firstName.equals(name)) list.add(e); | |
} | |
return list; | |
} | |
public Collection<Double> extract(Collection<Employee> collection) { | |
Collection<Double> list = new ArrayList<>(); | |
if (collection == null || collection.isEmpty()) return list; | |
for (Employee e : collection) { | |
list.add(e.salary); | |
} | |
return list; | |
} | |
// usage | |
double sal = max(extract(findName(collection, "Bob"))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment