List 中去除null方法

先看下面的程序段:

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.     List<Integer> arrays = new ArrayList<Integer>();  
  3.     arrays.add(2);  
  4.     arrays.add(null);  
  5.     arrays.add(456);  
  6.     arrays.add(null);  
  7.     arrays.add(789);  
  8.     System.out.println(arrays);  
  9. }  

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

 

 

Java代码   收藏代码
  1. [2, null, 456, null, 789]  

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

 

 

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.     List<Integer> arrays = new ArrayList<Integer>();  
  3.     arrays.add(2);  
  4.     arrays.add(null);  
  5.     arrays.add(456);  
  6.     arrays.add(null);  
  7.     arrays.add(789);  
  8.     arrays.remove(null);  
  9.     System.out.println(arrays);  
  10. }  

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

 

 

Java代码   收藏代码
  1. [2, 456, null, 789]  

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

 

 

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.     List<Integer> arrays = new ArrayList<Integer>();  
  3.     arrays.add(2);  
  4.     arrays.add(null);  
  5.     arrays.add(456);  
  6.     arrays.add(null);  
  7.     arrays.add(789);  
  8.     List<Integer> nullArr = new ArrayList<Integer>();  
  9.     nullArr.add(null);  
  10.     arrays.removeAll(nullArr);  
  11.     System.out.println(arrays);  
  12. }  

 程序的输出如下:

 

 

Java代码   收藏代码
  1. [2, 456, 789]  

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

 

 

Java代码   收藏代码
  1. arrays.removeAll(null);  

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

 

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

 

Java代码   收藏代码
  1. List<TempProductDto> nullTempProd = new ArrayList<TempProductDto>(1);  
  2. nullTempProd.add(null);  
  3.   
  4. List<MerchantDto> nullMerchant = new ArrayList<MerchantDto>(1);  
  5. nullMerchant.add(null);  

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

 

 

Java代码   收藏代码
  1. import java.io.Serializable;  
  2. import java.util.AbstractList;  
  3. import java.util.RandomAccess;  
  4.   
  5. public class NullCollection extends AbstractList<Object>  
  6. implements RandomAccess, Serializable  {  
  7.   
  8.     private static final long serialVersionUID = 5206887786441397812L;  
  9.   
  10.     @Override  
  11.     public Object get(int index) {  
  12.         return null;  
  13.     }  
  14.   
  15.     @Override  
  16.     public int size() {  
  17.         return 1;  
  18.     }  
  19.       
  20.     public boolean contains(Object obj) {  
  21.         return null == obj;  
  22.     }  
  23.       
  24.     private Object readResolve() {  
  25.         return null;  
  26.     }  
  27. }  

 

Java代码   收藏代码
  1. import java.util.Collection;  
  2. import java.util.List;  
  3.   
  4. public class YHDCollectionUtils {  
  5.       
  6.      public static final Collection NULL_COLLECTION = new NullCollection();  
  7.           
  8.     public static final <T> Collection<T> nullCollection() {  
  9.         return (List<T>) NULL_COLLECTION;  
  10.     }  
  11. }  

 

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

 

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.     List<Integer> arrays = new ArrayList<Integer>();  
  3.     arrays.add(2);  
  4.     arrays.add(null);  
  5.     arrays.add(456);  
  6.     arrays.add(null);  
  7.     arrays.add(789);  
  8.     arrays.removeAll(YHDCollectionUtils.nullCollection());  
  9.     System.out.println(arrays);  
  10. }  

 执行结果如下:

 

 

Java代码   收藏代码
  1. [2, 456, 789]  

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

 

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

 

Java代码   收藏代码
  1. <input name="dto.productList[0].name" value="我是名称1">  
  2. <input name="dto.productList[0].price" value="我是价格1">  
  3.   
  4. <input name="dto.productList[2].name" value="我是名称2">  
  5. <input name="dto.productList[2].price" value="我是价格2">  
  6.   
  7. <input name="dto.productList[4].name" value="我是名称3">  
  8. <input name="dto.productList[4].price" value="我是价格3">  

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

 

原文地址:http://www.iteye.com/topic/1132718

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值