import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main
(final String[] args
) { final List<Person> personsSortedByComparable = constructPersons(
new String[]{"Clair",
"Alice",
"Bob",
"Alice"},
new String[]{"Clear",
"Wonder",
"Builder",
"Ace"} );
System.
out.
println(personsSortedByComparable
);
final List<Person> personsSortByPersonByFirstNameThenByLastNameComparator =
constructPersons(
new String[]{"Clair",
"Alice",
"Bob",
"Alice"},
new String[]{"Clear",
"Wonder",
"Builder",
"Ace"} );
personsSortByPersonByFirstNameThenByLastNameComparator
.sort(PersonByFirstNameThenByLastNameComparator.INSTANCE);
System.
out.
println(personsSortByPersonByFirstNameThenByLastNameComparator
);
final List<Person> personsSortByComparator = constructPersons(
new String[]{"Clair",
"Alice",
"Bob",
"Alice"},
new String[]{"Clear",
"Wonder",
"Builder",
"Ace"} );
personsSortByComparator.
sort(Comparator.
comparing(Person
::getFirstName
) .thenComparing(Person::getLastName));
System.
out.
println(personsSortByComparator
); }
public static List<Person> constructPersons(
if (firstNames.length != lastNames.length) {
}
return IntStream.range(0, firstNames.length)
.mapToObj(index -> Person.of(firstNames[index], lastNames[index]))
.
collect(Collectors.
toCollection(ArrayList::new)); }
}
class Person implements Comparable<Person> {
public static final String PERSON_TO_STRING_FORMAT
= "{f: %s, l: %s}";
private final String firstName
; private final String lastName
;
private Person
(final String firstName,
final String lastName
) { this.firstName = Objects.requireNonNull(firstName);
this.lastName = Objects.requireNonNull(lastName);
}
public static Person of
(final String firstName,
final String lastName
) { return new Person(firstName, lastName);
}
public String getFirstName
() { return firstName;
}
return lastName;
}
@Override
return String.
format(PERSON_TO_STRING_FORMAT, getFirstName
(), getLastName
()); }
@Override
public final boolean equals
(Object thatObject
) { if (this == thatObject) {
return true;
}
if (thatObject == null || getClass() != thatObject.getClass()) {
return false;
}
final Person that = (Person) thatObject;
return Objects.equals(getFirstName(), that.getFirstName()) &&
Objects.equals(getLastName(), that.getLastName());
}
@Override
public final int hashCode() {
return Objects.hash(getFirstName(), getLastName());
}
@Override
public final int compareTo(final Person that) {
if (Objects.equals(getFirstName(), that.getFirstName())) {
return getLastName().compareTo(that.getLastName());
}
return getFirstName().compareTo(that.getFirstName());
}
}
class PersonByFirstNameThenByLastNameComparator implements Comparator<Person> {
public static final PersonByFirstNameThenByLastNameComparator INSTANCE =
new PersonByFirstNameThenByLastNameComparator();
private PersonByFirstNameThenByLastNameComparator() {
}
@Override
public int compare(final Person lhs, final Person rhs) {
if (Objects.equals(lhs.getFirstName(), rhs.getFirstName())) {
return lhs.getLastName().compareTo(rhs.getLastName());
}
return lhs.getFirstName().compareTo(rhs.getFirstName());
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS51dGlsLnN0cmVhbS5Db2xsZWN0b3JzOwppbXBvcnQgamF2YS51dGlsLnN0cmVhbS5JbnRTdHJlYW07CgpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihmaW5hbCBTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgZmluYWwgTGlzdDxQZXJzb24+IHBlcnNvbnNTb3J0ZWRCeUNvbXBhcmFibGUgPSBjb25zdHJ1Y3RQZXJzb25zKAogICAgICAgICAgICAgICAgbmV3IFN0cmluZ1tdeyJDbGFpciIsICJBbGljZSIsICJCb2IiLCAiQWxpY2UifSwKICAgICAgICAgICAgICAgIG5ldyBTdHJpbmdbXXsiQ2xlYXIiLCAiV29uZGVyIiwgIkJ1aWxkZXIiLCAiQWNlIn0KICAgICAgICApOwogICAgICAgIENvbGxlY3Rpb25zLnNvcnQocGVyc29uc1NvcnRlZEJ5Q29tcGFyYWJsZSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHBlcnNvbnNTb3J0ZWRCeUNvbXBhcmFibGUpOwoKICAgICAgICBmaW5hbCBMaXN0PFBlcnNvbj4gcGVyc29uc1NvcnRCeVBlcnNvbkJ5Rmlyc3ROYW1lVGhlbkJ5TGFzdE5h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FBlcnNvbiByaHMpIHsKICAgICAgICBpZiAoT2JqZWN0cy5lcXVhbHMobGhzLmdldEZpcnN0TmFtZSgpLCByaHMuZ2V0Rmlyc3ROYW1lKCkpKSB7CiAgICAgICAgICAgIHJldHVybiBsaHMuZ2V0TGFzdE5hbWUoKS5jb21wYXJlVG8ocmhzLmdldExhc3ROYW1lKCkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gbGhzLmdldEZpcnN0TmFtZSgpLmNvbXBhcmVUbyhyaHMuZ2V0Rmlyc3ROYW1lKCkpOwogICAgfQp9
[{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}]