44import org .junit .Test ;
55
66import java .util .List ;
7+ import java .util .Objects ;
78import java .util .stream .Collectors ;
89
910import static org .hamcrest .Matchers .hasSize ;
@@ -16,31 +17,37 @@ public class Java8CollectionCleanupUnitTest {
1617 @ Test
1718 public void givenListContainsNulls_whenFilteringParallel_thenCorrect () {
1819 final List <Integer > list = Lists .newArrayList (null , 1 , 2 , null , 3 , null );
19- final List <Integer > listWithoutNulls = list .parallelStream ().filter (i -> i != null ).collect (Collectors .toList ());
20+ final List <Integer > listWithoutNulls = list .parallelStream ()
21+ .filter (Objects ::nonNull )
22+ .collect (Collectors .toList ());
2023
2124 assertThat (listWithoutNulls , hasSize (3 ));
2225 }
2326
2427 @ Test
2528 public void givenListContainsNulls_whenFilteringSerial_thenCorrect () {
2629 final List <Integer > list = Lists .newArrayList (null , 1 , 2 , null , 3 , null );
27- final List <Integer > listWithoutNulls = list .stream ().filter (i -> i != null ).collect (Collectors .toList ());
30+ final List <Integer > listWithoutNulls = list .stream ()
31+ .filter (Objects ::nonNull )
32+ .collect (Collectors .toList ());
2833
2934 assertThat (listWithoutNulls , hasSize (3 ));
3035 }
3136
3237 @ Test
3338 public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect () {
3439 final List <Integer > listWithoutNulls = Lists .newArrayList (null , 1 , 2 , null , 3 , null );
35- listWithoutNulls .removeIf (p -> p == null );
40+ listWithoutNulls .removeIf (Objects :: isNull );
3641
3742 assertThat (listWithoutNulls , hasSize (3 ));
3843 }
3944
4045 @ Test
4146 public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect () {
4247 final List <Integer > listWithDuplicates = Lists .newArrayList (1 , 1 , 2 , 2 , 3 , 3 );
43- final List <Integer > listWithoutDuplicates = listWithDuplicates .parallelStream ().distinct ().collect (Collectors .toList ());
48+ final List <Integer > listWithoutDuplicates = listWithDuplicates .parallelStream ()
49+ .distinct ()
50+ .collect (Collectors .toList ());
4451
4552 assertThat (listWithoutDuplicates , hasSize (3 ));
4653 }
0 commit comments