11package com .baeldung .java8 ;
22
3- import static org .hamcrest .Matchers .equalTo ;
3+ import com .baeldung .java8 .entity .Human ;
4+ import com .google .common .collect .Lists ;
5+ import com .google .common .primitives .Ints ;
6+ import org .junit .Assert ;
7+ import org .junit .Test ;
48
59import java .util .Collections ;
610import java .util .Comparator ;
711import java .util .List ;
812import java .util .stream .Collectors ;
913
10- import org .junit .Assert ;
11- import org .junit .Test ;
12-
13- import com .baeldung .java8 .entity .Human ;
14- import com .google .common .collect .Lists ;
15- import com .google .common .primitives .Ints ;
14+ import static org .hamcrest .Matchers .equalTo ;
1615
1716public class Java8SortUnitTest {
1817
@@ -113,11 +112,11 @@ public final void givenInstanceMethod_whenSortingEntitiesByName_thenCorrectlySor
113112 humans .sort (Comparator .comparing (Human ::getName ));
114113 Assert .assertThat (humans .get (0 ), equalTo (new Human ("Jack" , 12 )));
115114 }
116-
115+
117116 @ Test
118117 public final void givenStreamNaturalOrdering_whenSortingEntitiesByName_thenCorrectlySorted () {
119118 final List <String > letters = Lists .newArrayList ("B" , "A" , "C" );
120-
119+
121120 final List <String > sortedLetters = letters .stream ().sorted ().collect (Collectors .toList ());
122121 Assert .assertThat (sortedLetters .get (0 ), equalTo ("A" ));
123122 }
@@ -126,7 +125,7 @@ public final void givenStreamNaturalOrdering_whenSortingEntitiesByName_thenCorre
126125 public final void givenStreamCustomOrdering_whenSortingEntitiesByName_thenCorrectlySorted () {
127126 final List <Human > humans = Lists .newArrayList (new Human ("Sarah" , 10 ), new Human ("Jack" , 12 ));
128127 final Comparator <Human > nameComparator = (h1 , h2 ) -> h1 .getName ().compareTo (h2 .getName ());
129-
128+
130129 final List <Human > sortedHumans = humans .stream ().sorted (nameComparator ).collect (Collectors .toList ());
131130 Assert .assertThat (sortedHumans .get (0 ), equalTo (new Human ("Jack" , 12 )));
132131 }
@@ -164,4 +163,48 @@ public final void givenStreamComparatorOrdering_whenSortingEntitiesByNameReverse
164163 Assert .assertThat (reverseSortedHumans .get (0 ), equalTo (new Human ("Sarah" , 10 )));
165164 }
166165
166+ @ Test (expected = NullPointerException .class )
167+ public final void givenANullElement_whenSortingEntitiesByName_thenThrowsNPE () {
168+ final List <Human > humans = Lists .newArrayList (null , new Human ("Jack" , 12 ));
169+
170+ humans .sort ((h1 , h2 ) -> h1 .getName ().compareTo (h2 .getName ()));
171+ }
172+
173+ @ Test
174+ public final void givenANullElement_whenSortingEntitiesByNameManually_thenMovesTheNullToLast () {
175+ final List <Human > humans = Lists .newArrayList (null , new Human ("Jack" , 12 ), null );
176+
177+ humans .sort ((h1 , h2 ) -> {
178+ if (h1 == null ) return h2 == null ? 0 : 1 ;
179+ else if (h2 == null ) return -1 ;
180+
181+ return h1 .getName ().compareTo (h2 .getName ());
182+ });
183+
184+ Assert .assertNotNull (humans .get (0 ));
185+ Assert .assertNull (humans .get (1 ));
186+ Assert .assertNull (humans .get (2 ));
187+ }
188+
189+ @ Test
190+ public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToLast () {
191+ final List <Human > humans = Lists .newArrayList (null , new Human ("Jack" , 12 ), null );
192+
193+ humans .sort (Comparator .nullsLast (Comparator .comparing (Human ::getName )));
194+
195+ Assert .assertNotNull (humans .get (0 ));
196+ Assert .assertNull (humans .get (1 ));
197+ Assert .assertNull (humans .get (2 ));
198+ }
199+
200+ @ Test
201+ public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToStart () {
202+ final List <Human > humans = Lists .newArrayList (null , new Human ("Jack" , 12 ), null );
203+
204+ humans .sort (Comparator .nullsFirst (Comparator .comparing (Human ::getName )));
205+
206+ Assert .assertNull (humans .get (0 ));
207+ Assert .assertNull (humans .get (1 ));
208+ Assert .assertNotNull (humans .get (2 ));
209+ }
167210}
0 commit comments