先看下面的程序段:
- 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 引起的空指针异常。
原文地址:http://www.iteye.com/topic/1132718
相关推荐
spring mvc拦截器,过滤json数据中的null值,将null变成空字符串,内含截图,及jar包
NULL 博文链接:https://xuedong.iteye.com/blog/1157186
NULL 博文链接:https://houlc.iteye.com/blog/2020481
主要介绍了如何实现java8 list按照元素的某个字段去重,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
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 ...
就是简单的将tag_list中的内容,即“paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));”中放入的数据遍历出来 style_2-1.html中的内容 主要是对图文列表或标题列表向上滚动的样式的,其中包含两个同样为样式...
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之...在 IUserOperation 接口中增加方法:public List...
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 ...
6.6 在代理中托管特殊方法 234 6.7 有命名子项的元组 237 6.8 避免属性读写的冗余代码 239 6.9 快速复制对象 240 6.10 保留对被绑定方法的引用且支持垃圾回收 243 6.11 缓存环的实现 245 6.12 检查一个实例的...
arcgis工具总结 ...这种方法选择某一图层中包含另一图层中要素的要素。这种方法与完全包含(Completely contain)方法的区别在于:要素间的边界可以接触。例如,使用包含(Contain)方法,即使湖泊的边界和包含该...
list指令是一个迭代输出指令,用于迭代输出数据模型中的集合,list指令的语法格式如下: <#list sequence as item> ... </#list> 上面的语法格式中,sequence就是一个集合对象,也可以是一个表达式,但该表达式将返回...
21、数组中有没有length()方法,String中有没有length()方法? 18 23、final, finally, finalize的区别。 18 24、‘==’和equals的区别? 18 25、JAVA中Object类中有哪些常用方法? 19 26、heap和stack有什么区别...
16.8.节使用ActionScript动态添加和去除图表中的列 16.9.节重叠多个图表 16.10.节拖曳图表中的项目 16.11.节创建一个可以编辑线状图 第十七章. 共享对象(557) 17.1节. 创建一个共享对象 17.2节. 写入数据到共享...
说明: 此参数指定链接程序 (如: UNIX 中的 ld, 或用于将目标文件链接到共享对象或 DLL 的 GNU ld) 的完整路径名。此参数是可选的。随每个平台附带的特有的 make 文件中包含此参数的默认值。如果为此参数指定了一个值...