kinow commented on a change in pull request #267:
URL:
https://github.com/apache/commons-collections/pull/267#discussion_r757770660
##########
File path: src/test/java/org/apache/commons/collections4/ListUtilsTest.java
##########
@@ -500,4 +500,45 @@ public void testSubtractNullElement() {
assertEquals(expected, result);
}
+
+ @Test
+ public void testremoveNullElements() {
Review comment:
The test name would have to be corrected to `testRemoveNullElements`
(upper case `R` in remove)
##########
File path: src/main/java/org/apache/commons/collections4/ListUtils.java
##########
@@ -744,6 +745,23 @@ public static String longestCommonSubsequence(final
CharSequence charSequenceA,
return UnmodifiableList.unmodifiableList(list);
}
+ /**
+ * Returns a new list by removing all null elements from the given list.
+ *
+ * @param <E> the element type
+ * @param list the input list, must not be null and must not be empty
+ * @return a new list by removing all null elements from the given list
+ * @throws NullPointerException if the input list is null
+ * @throws IllegalArgumentException if the input list is empty
+ */
+ public static <E> List<E> removeNullElements(final List<E> list) {
+ Objects.requireNonNull(list, "input list must not be null");
+ if (list.size() == 0) {
+ throw new IllegalArgumentException("input list must not be empty");
+ }
+ return
list.stream().filter(Objects::nonNull).collect(Collectors.toList());
+ }
Review comment:
Not sure if that would be worth adding this method. I think at the
moment users can use `ListUtils.removeAll(list, Arrays.astList(null))` (not
tested)? While `removeNullElements` could be convenient for some users, we
would have to consider the maintenance in commons-collections, and cases where
users want to remove not only `null`, but maybe also empty strings, invalid
values, etc. Let's wait to hear others :+1: thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]