`
bo_hai
  • 浏览: 553951 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

List 中去除 null 方法讨论

阅读更多

先看下面的程序段:

 

public static void main(String[] args) {
	List<Integer> arrays = new ArrayList<Integer>();
	arrays.add(2);
	arrays.add(null);
	arrays.add(456);
	arrays.add(null);
	arrays.add(789);
	System.out.println(arrays);
}

 注:一个list,向其中插入数据时,也插入一些null。程序输出如下:

 

 

[2, null, 456, null, 789]

 现在有这个需求:去除list中null 元素。尝试的代码如下:

 

 

public static void main(String[] args) {
	List<Integer> arrays = new ArrayList<Integer>();
	arrays.add(2);
	arrays.add(null);
	arrays.add(456);
	arrays.add(null);
	arrays.add(789);
	arrays.remove(null);
	System.out.println(arrays);
}

 调用remove(object)方法,程序的输出如下:

 

 

[2, 456, null, 789]

 可以看出:只remove了第一个null元素。这不是我们期望的结果。继续找方法。考虑到有一个removeAll(Collection<?> c) ,尝试使用。代码如下:

 

 

public static void main(String[] args) {
	List<Integer> arrays = new ArrayList<Integer>();
	arrays.add(2);
	arrays.add(null);
	arrays.add(456);
	arrays.add(null);
	arrays.add(789);
	List<Integer> nullArr = new ArrayList<Integer>();
	nullArr.add(null);
	arrays.removeAll(nullArr);
	System.out.println(arrays);
}

 程序的输出如下:

 

 

[2, 456, 789]

 这是我们期望的结果。你可能会尝试下面这样使用:

 

 

arrays.removeAll(null);

 很遗憾,程序出错了:Exception in thread "main" java.lang.NullPointerException。

 

 到这里,我们似乎找到了解决问题的办法。但是,如果我们的系统中,有这种类型的List<E>,如List<TempProductDto>、List<merchantDto> 时,我们要从这些List中移除掉null,就要创建如下的代码:

 

List<TempProductDto> nullTempProd = new ArrayList<TempProductDto>(1);
nullTempProd.add(null);

List<MerchantDto> nullMerchant = new ArrayList<MerchantDto>(1);
nullMerchant.add(null);

 每种类型,就要创建对应类型的List,并把null 放入到List中。是不是很麻烦。能不能写个公用的Util类呢?以下是我写的Util 类:

 

 

import java.io.Serializable;
import java.util.AbstractList;
import java.util.RandomAccess;

public class NullCollection extends AbstractList<Object>
implements RandomAccess, Serializable  {

	private static final long serialVersionUID = 5206887786441397812L;

	@Override
	public Object get(int index) {
		return null;
	}

	@Override
	public int size() {
		return 1;
	}
	
	public boolean contains(Object obj) {
		return null == obj;
	}
	
	private Object readResolve() {
        return null;
    }
}

 

import java.util.Collection;
import java.util.List;

public class YHDCollectionUtils {
	
	 public static final Collection NULL_COLLECTION = new NullCollection();
		
	public static final <T> Collection<T> nullCollection() {
		return (List<T>) NULL_COLLECTION;
	}
}

 

 使用我写的util类进行测试。代码如下:

 

public static void main(String[] args) {
	List<Integer> arrays = new ArrayList<Integer>();
	arrays.add(2);
	arrays.add(null);
	arrays.add(456);
	arrays.add(null);
	arrays.add(789);
	arrays.removeAll(YHDCollectionUtils.nullCollection());
	System.out.println(arrays);
}

 执行结果如下:

 

 

[2, 456, 789]

 Util 类可以成功的去除List中的null元素。

 

 也许你会问:为什么要把null放入List中,只有2B青年会这么干?在一般业务中,我们确实不需要把null放入List中,但有一种场景:从页面封装的List,如下面的代码:

 

<input name="dto.productList[0].name" value="我是名称1">
<input name="dto.productList[0].price" value="我是价格1">

<input name="dto.productList[2].name" value="我是名称2">
<input name="dto.productList[2].price" value="我是价格2">

<input name="dto.productList[4].name" value="我是名称3">
<input name="dto.productList[4].price" value="我是价格3">

 OGNL 会自动把dto.productList[1]、dto.productList[3] 的object封装成null。因此,我们在操作dto.productList 前,优先把 productList 中null去除掉,防止 null 引起的空指针异常。

 最后,欢迎各位拍砖。

 

分享到:
评论
1 楼 bo_hai 2019-03-15  
JDK1.8后,解决问题的办法就简单啦:
List<Integer> list = arrays.stream().filter(Objects::nonNull).collect(Collectors.toList());

相关推荐

Global site tag (gtag.js) - Google Analytics