ramananravi commented on a change in pull request #267:
URL: 
https://github.com/apache/commons-collections/pull/267#discussion_r757808884



##########
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:
       Agreed @garydgregory. The very fact that I had to put not-null checks in 
the middle of iterating a list every time, made me think if there could be a 
handy util to solve this (so that once done my list would become null-free). 
Again, I wouldn't say it's a must-have but thought if this could be a 
good-to-have util. On a side note, any alternate approach suggestions to avoid 
the said stream-lambda-collector pipeline? Just to know if there's a better way 
to do the same. Thanks in advance. 




-- 
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]


Reply via email to