list中除去所有null值

  • List.remove(Object o):刪除一個元素,成功則返回true;需要注意它只刪除一個;
  • List.removeAll(Collection<?> c):刪除存在集合c的所有情況,注意入參不是一個元素;
  • List.removeIf(Predicate<? super E> filter):刪除所有滿足條件的元素,入參爲Lambda表達式。

 例如:list.removeAll(Collections.singletonList(null));

Stream方法

   List<String> result = list.parallelStream() .filter(Objects::nonNull) .collect(Collectors.toList());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章