【问题标题】:Removing elements from List containing null using Iterator使用迭代器从列表中删除包含 null 的元素
【发布时间】:2015-04-09 14:21:46
【问题描述】:
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(null);
list.add(22);
list.add(32);

System.out.println(list);

Iterator<Integer> iterator = list.iterator();

while(iterator.hasNext()){
        Integer a = iterator.next();
        System.out.print(a+" ");

        if(a.equals(32)){  
        iterator.remove();
        }
    }

O/p

[12, null, 22, 32]
12 null Exception in thread "main" java.lang.NullPointerException
at javed.collection.Collecting.main(Collecting.java:57)

试图从 List 中删除任何包含 null 的元素会给我 NullPointerException 。 但是可以很容易地从不包含 null 的 List 中删除元素。 通过将整数与 null 进行比较,我做错了吗?

如果我们不能从 List 中删除包含 null 的元素而不是他们允许我们在 List 中添加 null 的原因,那么它有什么用处。

【问题讨论】:

  • 为什么您确定问题出在remove 行?

标签: java collections null core


【解决方案1】:

你应该添加一个空检查:

if(a != null && a.equals(32)) {
    iterator.remove();
}

您还可以使用 Objects.equals 为您执行 null 检查:

if(Objects.equals(a, 32)) {

【讨论】:

    【解决方案2】:

    将您的条件语句更改为:

    if(a.equals(32)){  
       iterator.remove();
    }
    

    到:

    Integer toCheck = 32;
    if(toCheck.equals(a)){  
        iterator.remove();
    }
    

    为了避免可能的空引用

    【讨论】:

      【解决方案3】:

      如果您在 null 变量上调用任何方法,您将得到一个 NullPointerException。这意味着如果 anull 并且您尝试调用 a.equals(32),它将是 NPE。

      您可以通过简单地输入来对变量进行空检查:

      if (a!=null && a==32) {
          //whatever
      }
      

      另一方面,您不必手动遍历列表来从中删除特定元素。有一种方法可以为您做到:

      list.remove(Integer.valueOf(32));
      

      【讨论】:

        【解决方案4】:

        您绝对可以从包含空引用的列表中删除 - 您不能在空引用上调用 equals。问题出在这里:

        if(a.equals(32))
        

        如果anull,则NullPointerException 将失败。 没有列表是关于那个的。您可以在没有列表或迭代器的情况下获得完全相同的行为:

        Integer a = null;
        if (a.equals(32)) // Bang!
            ...
        

        最简单的修复方法是检查无效性:

        if (a != null && a == 32)
        

        (现在比较使用取消装箱,而不是每次迭代都装箱 32 - 这仍然很便宜,但不像 IMO 那样可读。)

        或者有一个装箱的 Integer 值开始:

        Integer valueToRemove = 32;
        while(iterator.hasNext()){
            Integer a = iterator.next();
            System.out.print(a+" ");
        
            if(valueToRemove.equals(a)) {
                iterator.remove();
            }
        }
        

        【讨论】:

        • 嘿thanx,如果 (a != null && a == 32) 这很好用。
        【解决方案5】:

        你可以这样做:

        if (a != null && a == 32) {
            iterator.remove();
        }
        

        但这并不是一个真正的好习惯,因为a 的隐含框/debox。

        最快的方法是:

        Iterator<Integer> iterator = list.iterator();
        Integer matcher = 32;
        while(iterator.hasNext()){
            Integer a = iterator.next();
            System.out.print(a+" ");
        
            if(matcher.equals(a)){  
                iterator.remove();
            }
        }
        

        【讨论】:

          【解决方案6】:

          此行将从列表中删除所有空值

              list.removeAll(Collections.singleton(null));
          

          【讨论】:

            猜你喜欢
            • 2011-10-16
            • 2013-11-25
            • 1970-01-01
            • 2016-07-19
            • 1970-01-01
            • 2020-03-18
            • 2019-09-11
            • 2018-07-18
            • 2021-03-01
            相关资源
            最近更新 更多