fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7. public static class Person {
  8. private String firstName, lastName;
  9. private Integer age;
  10.  
  11. public Person(String firstName, String lastName, Integer age){
  12. this.firstName = Objects.requireNonNull(firstName);
  13. this.lastName = Objects.requireNonNull(lastName);
  14. this.age = age;
  15. }
  16.  
  17. public void setFirstName(String firstname) {
  18. this.firstName = Objects.requireNonNull(firstName);
  19. }
  20. public String getFirstName(){ return firstName;}
  21.  
  22. public void setLastName(String lastName) {
  23. this.lastName = Objects.requireNonNull(lastName);
  24. }
  25. public String getLastName() { return lastName;}
  26.  
  27. public void setAge(Integer age) { this.age=age;}
  28. public Integer getAge(){ return age;}
  29.  
  30. public String toString(){
  31. return firstName+" "+lastName+" with age:"+age;
  32. }
  33.  
  34. }
  35.  
  36. public static void main (String[] args) throws java.lang.Exception
  37. {
  38. List<Person> people = new ArrayList<>();
  39. people.add(new Person("Nom","Faux",null));
  40. people.add(new Person("Toto", "Tata", 32));
  41. people.add(new Person("Nom", "Prenom", 44));
  42. people.add(new Person("Nom", "Prenom", null));
  43. Comparator<Person> comparator = Comparator
  44. .comparing(Person::getFirstName)
  45. .thenComparing(Person::getLastName)
  46. .thenComparing(Person::getAge, Comparator.nullsLast(Integer::compareTo));
  47. Collections.sort(people, comparator);
  48. people.stream().forEach(System.out::println);
  49. }
  50. }
Success #stdin #stdout 0.09s 842752KB
stdin
Standard input is empty
stdout
Nom Faux with age:null
Nom Prenom with age:44
Nom Prenom with age:null
Toto Tata with age:32