less verbose comparing logic
This commit is contained in:
parent
081b926939
commit
7a6439c8e5
@ -63,7 +63,7 @@ public class Java8SortUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
|
public final void givenCompositionVerbose_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
|
||||||
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
|
||||||
final Comparator<Human> byName = (h1, h2) -> h1.getName().compareTo(h2.getName());
|
final Comparator<Human> byName = (h1, h2) -> h1.getName().compareTo(h2.getName());
|
||||||
final Comparator<Human> byAge = (h1, h2) -> Ints.compare(h1.getAge(), h2.getAge());
|
final Comparator<Human> byAge = (h1, h2) -> Ints.compare(h1.getAge(), h2.getAge());
|
||||||
@ -72,6 +72,14 @@ public class Java8SortUnitTest {
|
|||||||
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
|
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
|
||||||
|
|
||||||
|
humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
|
||||||
|
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingEntitiesByAge_thenCorrectlySorted() {
|
public final void whenSortingEntitiesByAge_thenCorrectlySorted() {
|
||||||
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user