new tests
This commit is contained in:
parent
ba8559ab50
commit
6633f5b9a0
@ -1,8 +1,10 @@
|
|||||||
package org.baeldung.java8;
|
package org.baeldung.java8;
|
||||||
|
|
||||||
|
import static org.baeldung.java8.entity.Human.compareByNameThenAge;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.java8.entity.Human;
|
import org.baeldung.java8.entity.Human;
|
||||||
@ -16,6 +18,18 @@ public class Java8SortUnitTest {
|
|||||||
|
|
||||||
// tests -
|
// tests -
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenPreLambda_whenSortingEntitiesByName_thenCorrectlySorted() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
|
Collections.sort(humans, new Comparator<Human>() {
|
||||||
|
@Override
|
||||||
|
public final int compare(final Human h1, final Human h2) {
|
||||||
|
return h1.getName().compareTo(h2.getName());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingEntitiesByName_thenCorrectlySorted() {
|
public final void whenSortingEntitiesByName_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));
|
||||||
@ -24,10 +38,61 @@ public class Java8SortUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingEntitiesByAge_thenCorrectlySorted() {
|
public final void givenLambdaShortForm_whenSortingEntitiesByName_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));
|
||||||
Collections.sort(humans, (final Human h1, final Human h2) -> Ints.compare(h1.getAge(), h2.getAge()));
|
Collections.sort(humans, (h1, h2) -> h1.getName().compareTo(h2.getName()));
|
||||||
|
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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) -> {
|
||||||
|
if (lhs.getName().equals(rhs.getName())) {
|
||||||
|
return lhs.getAge() - rhs.getAge();
|
||||||
|
} else {
|
||||||
|
return lhs.getName().compareTo(rhs.getName());
|
||||||
|
}
|
||||||
|
});
|
||||||
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));
|
||||||
|
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));
|
||||||
|
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()));
|
||||||
|
Assert.assertThat(humans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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());
|
||||||
|
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);
|
||||||
|
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));
|
||||||
|
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,16 @@ public class Human {
|
|||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compare
|
||||||
|
|
||||||
|
public static int compareByNameThenAge(final Human lhs, final Human rhs) {
|
||||||
|
if (lhs.name.equals(rhs.name)) {
|
||||||
|
return lhs.age - rhs.age;
|
||||||
|
} else {
|
||||||
|
return lhs.name.compareTo(rhs.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user