diff --git a/src/test/java/com/levelup/java/collections/FilterNullFromCollection.java b/src/test/java/com/levelup/java/collections/FilterNullFromCollection.java index 37389527..aaf5e962 100644 --- a/src/test/java/com/levelup/java/collections/FilterNullFromCollection.java +++ b/src/test/java/com/levelup/java/collections/FilterNullFromCollection.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertThat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -27,11 +28,12 @@ * @author Justin Musgrove * @see Filter null from collection */ +@SuppressWarnings("SuspiciousMethodCalls") public class FilterNullFromCollection { @Test - public void remove_null_from_list_java () { + public void remove_null_from_mutable_list_java() { List strings = new ArrayList<>(); strings.add(null); @@ -45,28 +47,43 @@ public void remove_null_from_list_java () { assertEquals(3, strings.size()); } + + + @Test(expected = UnsupportedOperationException.class) + public void try_remove_null_from_immutable_list_java() { + + List strings = Arrays.asList(null, "www", null, "leveluplunch", "com", null); + + strings.removeAll(Collections.singleton(null)); + } + + @Test + public void remove_null_from_immutable_list_java() { + + List strings = Arrays.asList(null, "www", null, "leveluplunch", "com", null); + + List filterStrings = new ArrayList(strings) { + { + removeAll(Collections.singleton(null)); + } + }; + + assertEquals(3, filterStrings.size()); + } @Test public void remove_null_from_list_java8_lambda () { List strings = Lists.newArrayList( - null, "www", null, + null, "www", null, "leveluplunch", "com", null); List filterStrings = strings - .stream() - .filter(p -> p != null) - .collect(Collectors.toList()); - - assertEquals(3, filterStrings.size()); - - // or - List filterStrings2 = strings .stream() .filter(Objects::nonNull) .collect(Collectors.toList()); - assertEquals(3, filterStrings2.size()); + assertEquals(3, filterStrings.size()); }