`
tiankong6622
  • 浏览: 53569 次
社区版块
存档分类
最新评论

List 中去除null方法

    博客分类:
  • java
阅读更多

先看下面的程序段:

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

 

分享到:
评论

相关推荐

    spring mvc拦截器过滤json中的null值

    spring mvc拦截器,过滤json数据中的null值,将null变成空字符串,内含截图,及jar包

    去掉list中的重复对象

    NULL 博文链接:https://xuedong.iteye.com/blog/1157186

    java list 过滤

    NULL 博文链接:https://houlc.iteye.com/blog/2020481

    如何实现java8 list按照元素的某个字段去重

    主要介绍了如何实现java8 list按照元素的某个字段去重,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下

    重构 改善既有代码的设计

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    总结C#删除字符串数组中空字符串的几种方法

    C#中要如何才能删除一个字符串数组中的空字符串呢?下面的文章会介绍多种方式来实现清除数组中的空字符串,以及在.net中将字符串数组中字符串为空的元素去除。

    重构_改善既有代码的设计[高清版]中文版

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    重构-改善既有代码的设计+中文版

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    重构——改善既有代码的设计

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    重构-改善既有代码的设计(中文版)

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    重构,改善既有代码的设计

     Remove Setting Method 去除设置方法   Hide Method 隐藏方法   Replace Constructor with Factory Method 用工厂方法代替构造器   Encapsulate Downcast 封装向下转型   Replace Error Code with ...

    cms后台管理

    就是简单的将tag_list中的内容,即“paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));”中放入的数据遍历出来 style_2-1.html中的内容 主要是对图文列表或标题列表向上滚动的样式的,其中包含两个同样为样式...

    springmybatis

    MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之...在 IUserOperation 接口中增加方法:public List...

    双向链表 链表(C++编写)

    The `prev' link of the front header is null, as is the `next' link of the back header. Their other two links point toward each other via the interior elements of the list. An empty list looks ...

    Python Cookbook

    6.6 在代理中托管特殊方法 234 6.7 有命名子项的元组 237 6.8 避免属性读写的冗余代码 239 6.9 快速复制对象 240 6.10 保留对被绑定方法的引用且支持垃圾回收 243 6.11 缓存环的实现 245 6.12 检查一个实例的...

    arcgis工具

    arcgis工具总结 ...这种方法选择某一图层中包含另一图层中要素的要素。这种方法与完全包含(Completely contain)方法的区别在于:要素间的边界可以接触。例如,使用包含(Contain)方法,即使湖泊的边界和包含该...

    freemarker总结

    list指令是一个迭代输出指令,用于迭代输出数据模型中的集合,list指令的语法格式如下: &lt;#list sequence as item&gt; ... &lt;/#list&gt; 上面的语法格式中,sequence就是一个集合对象,也可以是一个表达式,但该表达式将返回...

    Java面试宝典2020修订版V1.0.1.doc

    21、数组中有没有length()方法,String中有没有length()方法? 18 23、final, finally, finalize的区别。 18 24、‘==’和equals的区别? 18 25、JAVA中Object类中有哪些常用方法? 19 26、heap和stack有什么区别...

    flex3的cookbook书籍完整版dpf(包含目录)

    16.8.节使用ActionScript动态添加和去除图表中的列 16.9.节重叠多个图表 16.10.节拖曳图表中的项目 16.11.节创建一个可以编辑线状图 第十七章. 共享对象(557) 17.1节. 创建一个共享对象 17.2节. 写入数据到共享...

    Oracle9i的init.ora参数中文说明

    说明: 此参数指定链接程序 (如: UNIX 中的 ld, 或用于将目标文件链接到共享对象或 DLL 的 GNU ld) 的完整路径名。此参数是可选的。随每个平台附带的特有的 make 文件中包含此参数的默认值。如果为此参数指定了一个值...

Global site tag (gtag.js) - Google Analytics