Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created November 2, 2014 21:37
Show Gist options
  • Save vfarcic/1d683808bf3a8d6a7670 to your computer and use it in GitHub Desktop.
Save vfarcic/1d683808bf3a8d6a7670 to your computer and use it in GitHub Desktop.
package com.technologyconversations.java8exercises.streams;
import java.util.Comparator;
import java.util.List;
public class OldestPerson {
public static Person getOldestPerson7(List<Person> people) {
Person oldestPerson = new Person("", 0);
for (Person person : people) {
if (person.getAge() > oldestPerson.getAge()) {
oldestPerson = person;
}
}
return oldestPerson;
}
public static Person getOldestPerson(List<Person> people) {
return people.stream() // Convert collection to Stream
.max(Comparator.comparing(Person::getAge)) // Compares people ages
.get(); // Gets stream result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment