Merge pull request #8992 from alimate/BAEL-3922
BAEL-3922: Introducing nullsFirst and nullsLast
This commit is contained in:
commit
18eb5188ca
@ -1,18 +1,17 @@
|
|||||||
package com.baeldung.java8;
|
package com.baeldung.java8;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import com.baeldung.java8.entity.Human;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.primitives.Ints;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.baeldung.java8.entity.Human;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.primitives.Ints;
|
|
||||||
|
|
||||||
public class Java8SortUnitTest {
|
public class Java8SortUnitTest {
|
||||||
|
|
||||||
@ -164,4 +163,48 @@ public class Java8SortUnitTest {
|
|||||||
Assert.assertThat(reverseSortedHumans.get(0), equalTo(new Human("Sarah", 10)));
|
Assert.assertThat(reverseSortedHumans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public final void givenANullElement_whenSortingEntitiesByName_thenThrowsNPE() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12));
|
||||||
|
|
||||||
|
humans.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenANullElement_whenSortingEntitiesByNameManually_thenMovesTheNullToLast() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
|
||||||
|
|
||||||
|
humans.sort((h1, h2) -> {
|
||||||
|
if (h1 == null) return h2 == null ? 0 : 1;
|
||||||
|
else if (h2 == null) return -1;
|
||||||
|
|
||||||
|
return h1.getName().compareTo(h2.getName());
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.assertNotNull(humans.get(0));
|
||||||
|
Assert.assertNull(humans.get(1));
|
||||||
|
Assert.assertNull(humans.get(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToLast() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
|
||||||
|
|
||||||
|
humans.sort(Comparator.nullsLast(Comparator.comparing(Human::getName)));
|
||||||
|
|
||||||
|
Assert.assertNotNull(humans.get(0));
|
||||||
|
Assert.assertNull(humans.get(1));
|
||||||
|
Assert.assertNull(humans.get(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToStart() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
|
||||||
|
|
||||||
|
humans.sort(Comparator.nullsFirst(Comparator.comparing(Human::getName)));
|
||||||
|
|
||||||
|
Assert.assertNull(humans.get(0));
|
||||||
|
Assert.assertNull(humans.get(1));
|
||||||
|
Assert.assertNotNull(humans.get(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user