fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Person {
  6.  
  7. private String name;
  8. private String city;
  9. private int age;
  10.  
  11. public Person(String name, String city, int age) {
  12. this.name = name;
  13. this.city = city;
  14. this.age = age;
  15. }
  16.  
  17. public int getAge() {
  18. return age;
  19. }
  20.  
  21. public String toString() {
  22. return "["+name+", "+ city + ", " + age + "]";
  23. }
  24. }
  25.  
  26. class Persons {
  27. private Persons() {}
  28. static Optional<Person> oldest(Person[] input) {
  29. return Arrays.stream(input).sorted(Comparator.comparing(Person::getAge).reversed()).findFirst();
  30. }
  31. }
  32.  
  33. class Ideone
  34. {
  35. public static void main (String[] args) throws java.lang.Exception
  36. {
  37. ArrayList<Person> aList = new ArrayList<Person>() {{
  38.  
  39. add(new Person("Diana", "Dublin", 38));
  40. add(new Person("Arthur","New York", 49));
  41. add(new Person("Kim", "Katowice", 35));
  42. add(new Person("Ghaffar","Lublin", 36));
  43. add(new Person("Zach","Radom", 19));
  44. }};
  45.  
  46. System.out.println("The size of the list is:" + aList.size());
  47. System.out.println("Oldest is: " + Persons.oldest(aList.toArray(new Person[0])));
  48. }
  49. }
Success #stdin #stdout 0.17s 39312KB
stdin
Standard input is empty
stdout
The size of the list is:5
Oldest is: Optional[[Arthur, New York, 49]]