using the new sort API

This commit is contained in:
eugenp 2015-01-03 18:04:19 +02:00
parent 2d4ee75653
commit 081b926939
1 changed files with 12 additions and 7 deletions

View File

@ -35,7 +35,7 @@ public class Java8SortUnitTest {
public final void whenSortingEntitiesByName_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
Collections.sort(humans, (final Human h1, final Human h2) -> h1.getName().compareTo(h2.getName()));
humans.sort((final Human h1, final Human h2) -> h1.getName().compareTo(h2.getName()));
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
}
@ -52,7 +52,7 @@ public class Java8SortUnitTest {
@Test
public final void whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 12), new Human("Sarah", 10), new Human("Zack", 12));
Collections.sort(humans, (lhs, rhs) -> {
humans.sort((lhs, rhs) -> {
if (lhs.getName().equals(rhs.getName())) {
return lhs.getAge() - rhs.getAge();
} else {
@ -67,14 +67,16 @@ public class Java8SortUnitTest {
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> byAge = (h1, h2) -> Ints.compare(h1.getAge(), h2.getAge());
Collections.sort(humans, byName.thenComparing(byAge));
humans.sort(byName.thenComparing(byAge));
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
}
@Test
public final void whenSortingEntitiesByAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
Collections.sort(humans, (h1, h2) -> Ints.compare(h1.getAge(), h2.getAge()));
humans.sort((h1, h2) -> Ints.compare(h1.getAge(), h2.getAge()));
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
}
@ -82,21 +84,24 @@ public class Java8SortUnitTest {
public final void whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
final Comparator<Human> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
Collections.sort(humans, comparator.reversed());
humans.sort(comparator.reversed());
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
}
@Test
public final void givenMethodDefinition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
Collections.sort(humans, Human::compareByNameThenAge);
humans.sort(Human::compareByNameThenAge);
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
}
@Test
public final void givenInstanceMethod_whenSortingEntitiesByName_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
Collections.sort(humans, Comparator.comparing(Human::getName));
humans.sort(Comparator.comparing(Human::getName));
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
}