zoukankan      html  css  js  c++  java
  • 【Java必修课】四类方法删除List里面的所有null值

    1 简介

    万恶的null已经折磨程序员许久了,也带来了许多难以发现却造成严重损失的NullPointerException。我们需要尽可能的避免它,有一种简单的办法就是在它进入下轮处理前,我们就把它扼杀在摇篮里。

    本文介绍了四类方法,分别是List接口的方法、StreamGuavaApache Commons Collections来删除一个List里面的null值。希望读者可以举一反三,得到更多启发。

    2 四类方法

    2.1 List自带的方法

    List有许多remove的方法可以使用,以下三个都可以满足我们的需求:

    • List.remove(Object o):删除一个元素,成功则返回true;需要注意它只删除一个;
    • List.removeAll(Collection<?> c):删除存在集合c的所有情况,注意入参不是一个元素;
    • List.removeIf(Predicate<? super E> filter):删除所有满足条件的元素,入参为Lambda表达式。

    代码如下:

    @Test
    public void listRemove() {
      List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
      //remove
      while (list.remove(null));//巧妙利用循环删除
      assertEquals(expected, list);
      //removeAll
      list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      list.removeAll(Collections.singletonList(null));
      assertEquals(expected, list);
      //removeIf
      list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      list.removeIf(Objects::isNull);
      assertEquals(expected, list);
    }
    

    2.2 Stream的方式

    Stream的方法很容易理解,就是加一个过滤器即可,过滤条件为非空,具体代码如下:

    @Test
    public void stream() {
      List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
      List<String> result = list.parallelStream()
        .filter(Objects::nonNull)
        .collect(Collectors.toList());
      assertEquals(expected, result);
    }
    

    2.3 使用Guava库

    Guava是非常优秀的Java库,提供了许多优秀的处理集合类的方法,本次使用了Iterables类进行处理,代码如下:

    @Test
    public void guava() {
      //改变原有List
      List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
      Iterables.removeIf(list, Objects::isNull);
      assertEquals(expected, list);
      //保留原有List
      list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      List<String> result = Lists.newArrayList(Iterables.filter(list, Objects::nonNull));
      assertEquals(expected, result);
    }
    

    本文提供两种方法,一种会改变原有的List,另一种则不会。

    2.4 使用Apache Commons的库

    Apache Commons Collections也提供了很方便的方法,具体代码如下:

    @Test
    public void apacheCommonsCollections() {
      List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
      List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
      CollectionUtils.filter(list, Objects::nonNull);
      assertEquals(expected, list);
    }
    

    3 总结

    虽然本文讲的是List删除null的方法,但稍加修改,就可以扩展到集合类删除特定条件的值,这还是非常有用的。


    欢迎关注公众号<南瓜慢说>,将持续为你更新...

    多读书,多分享;多写作,多整理。

  • 相关阅读:
    文本数据清洗总结
    PyTorch
    PyTorch
    NLP
    TF
    TF
    TF
    cairosvg
    word2vec 实现 影评情感分析
    Gensim
  • 原文地址:https://www.cnblogs.com/larrydpk/p/11774414.html
Copyright © 2011-2022 走看看