fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.time.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone {
  8.  
  9. public static void main (String[] args) throws java.lang.Exception {
  10.  
  11. Person youngest;
  12.  
  13. // you could do string comparison if you know the timezones are consistent:
  14.  
  15. youngest = listOfPersons.stream()
  16. .max(Comparator.comparing(p -> p.dateOfBirth)).get();
  17.  
  18. System.out.println("youngest is: " + youngest.name + " " + youngest.dateOfBirth);
  19.  
  20. // or you could just parse the date, a safer option:
  21.  
  22. youngest = listOfPersons.stream()
  23. .max(Comparator.comparing(p -> LocalDateTime.parse(p.dateOfBirth))).get();
  24.  
  25. System.out.println("youngest is: " + youngest.name + " " + youngest.dateOfBirth);
  26.  
  27. }
  28.  
  29.  
  30. //------------------------------------
  31. // types from OP:
  32.  
  33. static class Person{
  34. String name;
  35. String id;
  36. String dateOfBirth;
  37. int salary;
  38. }
  39.  
  40. static List <Person> listOfPersons;
  41.  
  42.  
  43. //------------------------------------
  44. // boring example data initialization:
  45.  
  46. // make an iso birthdate string given an age (for initializing data)
  47. static String fromAge (int years) {
  48. return LocalDateTime.now().minusYears(years).toString(); // ISO8601
  49. }
  50.  
  51. // make up some data
  52. static {
  53. listOfPersons = List.of(
  54. // fine for example, but i wouldn't init like this in production
  55. new Person() {{ name="helga"; dateOfBirth=fromAge(22); }},
  56. new Person() {{ name="mortimer"; dateOfBirth=fromAge(48); }},
  57. new Person() {{ name="gertrude"; dateOfBirth=fromAge(6); }},
  58. new Person() {{ name="jebediah"; dateOfBirth=fromAge(39); }}
  59. );
  60. listOfPersons.forEach(p -> System.out.println(p.name + ' ' + p.dateOfBirth));
  61. }
  62.  
  63.  
  64. }
Success #stdin #stdout 0.2s 55648KB
stdin
Standard input is empty
stdout
helga 2000-06-22T14:56:39.329972
mortimer 1974-06-22T14:56:39.330483
gertrude 2016-06-22T14:56:39.331082
jebediah 1983-06-22T14:56:39.331653
youngest is: gertrude 2016-06-22T14:56:39.331082
youngest is: gertrude 2016-06-22T14:56:39.331082