sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

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;

}





}


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);
}
     the collection to subtract from, must not be null
     the collection to subtract, must not be null

自定义方法,移除一个集合中所有的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("指定集合不能为空");

}

}

}



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的集合,禁止传入nullArrayList&lt;T&gt; list = new ArrayList&lt;&gt;(source.size());

for (T value : source) {
  if (value != null) {
    list.add(value);
  }
}
return list;
     参数。
     参数名。
      当参数argument为null时抛出。
posted on   sunny123456  阅读(851)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示