sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Java集合中移除所有的null值
https://blog.csdn.net/qq_36135928/article/details/86605104

org.apache.commons.collections.subtract方法只能移除第一个null元素。

public class CollectionRemoveNullTest {

@Test
public void test() {
List<String> nullList = new ArrayList<>();
nullList.add("a");
nullList.add(null);
nullList.add("b");
nullList.add("c");
nullList.add(null);

Collection&lt;String&gt; removeNull = subtract(nullList, Arrays.asList((String) null));
Iterator&lt;String&gt; it = removeNull.iterator();
while (it.hasNext()) {
  String s = it.next();
  assertTrue(s != null);
}

}

/**

  • Returns a new {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>.
  • The cardinality of each element <i>e</i> in the returned
  • will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality of
  • <i>e</i> in <i>b</i>, or zero, whichever is greater.
  • @param a
  •      the collection to subtract from, must not be null
    
  • @param b
  •      the collection to subtract, must not be null
    
  • @return a new collection with the results
  • @see Collection#removeAll
    */
    public static Collection subtract(final Collection a, final Collection b) {
    ArrayList list = new ArrayList(a);
    for (Iterator it = b.iterator(); it.hasNext()😉 {
    list.remove(it.next());
    }
    return list;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

自定义方法,移除一个集合中所有的null值

public class CollectionRemoveNullTest {

@Test
public void test() {
List<String> nullList = new ArrayList<>();
nullList.add("a");
nullList.add(null);
nullList.add("b");
nullList.add("c");
nullList.add(null);

Collection&lt;String&gt; removeNull = removeNull(nullList);
Iterator&lt;String&gt; it = removeNull.iterator();
while (it.hasNext()) {
  String s = it.next();
  assertTrue(s != null);
}

}

/**

  • 移除指定集合中的所有null值。
  • @param source
  •      待移除null的集合,禁止传入null。
    
  • @return 新的结果集合。
    */
    public static <T> Collection<T> removeNull(final Collection<T> source) {
    assertArgumentNotNull(source, "source");
ArrayList&lt;T&gt; list = new ArrayList&lt;&gt;(source.size());

for (T value : source) {
  if (value != null) {
    list.add(value);
  }
}
return list;

}

/**

  • 断言参数不是null,若是null将抛出异常。
  • @param argument
  •      参数。
    
  • @param argumentName
  •      参数名。
    
  • @throws IllegalArgumentException
  •       当参数argument为null时抛出。
    
  • @since 1.17
    */
    public static void assertArgumentNotNull(Object argument, String argumentName)
    throws IllegalArgumentException {
    if (argument == null) {
    throw new IllegalArgumentException("指定集合不能为空");
    }
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
posted on 2022-06-08 22:24  sunny123456  阅读(823)  评论(0编辑  收藏  举报