fork download
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4.  
  5. public class Main {
  6. public static void main(final String[] args) {
  7. final List<Person> personsSortedByComparable = constructPersons(
  8. new String[]{"Clair", "Alice", "Bob", "Alice"},
  9. new String[]{"Clear", "Wonder", "Builder", "Ace"}
  10. );
  11. Collections.sort(personsSortedByComparable);
  12. System.out.println(personsSortedByComparable);
  13.  
  14. final List<Person> personsSortByPersonByFirstNameThenByLastNameComparator =
  15. constructPersons(
  16. new String[]{"Clair", "Alice", "Bob", "Alice"},
  17. new String[]{"Clear", "Wonder", "Builder", "Ace"}
  18. );
  19. personsSortByPersonByFirstNameThenByLastNameComparator
  20. .sort(PersonByFirstNameThenByLastNameComparator.INSTANCE);
  21. System.out.println(personsSortByPersonByFirstNameThenByLastNameComparator);
  22.  
  23. final List<Person> personsSortByComparator = constructPersons(
  24. new String[]{"Clair", "Alice", "Bob", "Alice"},
  25. new String[]{"Clear", "Wonder", "Builder", "Ace"}
  26. );
  27. personsSortByComparator.sort(Comparator.comparing(Person::getFirstName)
  28. .thenComparing(Person::getLastName));
  29. System.out.println(personsSortByComparator);
  30. }
  31.  
  32. public static List<Person> constructPersons(
  33. final String[] firstNames,
  34. final String[] lastNames) {
  35. if (firstNames.length != lastNames.length) {
  36. throw new IllegalArgumentException("firstNames and lastNames must have same length");
  37. }
  38. return IntStream.range(0, firstNames.length)
  39. .mapToObj(index -> Person.of(firstNames[index], lastNames[index]))
  40. .collect(Collectors.toCollection(ArrayList::new));
  41. }
  42. }
  43.  
  44. class Person implements Comparable<Person> {
  45. public static final String PERSON_TO_STRING_FORMAT = "{f: %s, l: %s}";
  46.  
  47. private final String firstName;
  48. private final String lastName;
  49.  
  50. private Person(final String firstName, final String lastName) {
  51. this.firstName = Objects.requireNonNull(firstName);
  52. this.lastName = Objects.requireNonNull(lastName);
  53. }
  54.  
  55. public static Person of(final String firstName, final String lastName) {
  56. return new Person(firstName, lastName);
  57. }
  58.  
  59. public String getFirstName() {
  60. return firstName;
  61. }
  62.  
  63. public String getLastName() {
  64. return lastName;
  65. }
  66.  
  67. @Override
  68. public String toString() {
  69. return String.format(PERSON_TO_STRING_FORMAT, getFirstName(), getLastName());
  70. }
  71.  
  72. @Override
  73. public final boolean equals(Object thatObject) {
  74. if (this == thatObject) {
  75. return true;
  76. }
  77. if (thatObject == null || getClass() != thatObject.getClass()) {
  78. return false;
  79. }
  80. final Person that = (Person) thatObject;
  81. return Objects.equals(getFirstName(), that.getFirstName()) &&
  82. Objects.equals(getLastName(), that.getLastName());
  83. }
  84.  
  85. @Override
  86. public final int hashCode() {
  87. return Objects.hash(getFirstName(), getLastName());
  88. }
  89.  
  90. @Override
  91. public final int compareTo(final Person that) {
  92. if (Objects.equals(getFirstName(), that.getFirstName())) {
  93. return getLastName().compareTo(that.getLastName());
  94. }
  95. return getFirstName().compareTo(that.getFirstName());
  96. }
  97. }
  98.  
  99. class PersonByFirstNameThenByLastNameComparator implements Comparator<Person> {
  100. public static final PersonByFirstNameThenByLastNameComparator INSTANCE =
  101. new PersonByFirstNameThenByLastNameComparator();
  102.  
  103. private PersonByFirstNameThenByLastNameComparator() {
  104. }
  105.  
  106. @Override
  107. public int compare(final Person lhs, final Person rhs) {
  108. if (Objects.equals(lhs.getFirstName(), rhs.getFirstName())) {
  109. return lhs.getLastName().compareTo(rhs.getLastName());
  110. }
  111. return lhs.getFirstName().compareTo(rhs.getFirstName());
  112. }
  113. }
Success #stdin #stdout 0.1s 35196KB
stdin
Standard input is empty
stdout
[{f: Alice, l: Ace}, {f: Alice, l: Wonder}, {f: Bob, l: Builder}, {f: Clair, l: Clear}]
[{f: Alice, l: Ace}, {f: Alice, l: Wonder}, {f: Bob, l: Builder}, {f: Clair, l: Clear}]
[{f: Alice, l: Ace}, {f: Alice, l: Wonder}, {f: Bob, l: Builder}, {f: Clair, l: Clear}]