先看下面的程序段:
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 引起的空指针异常。
最后,欢迎各位拍砖。
相关推荐
spring mvc拦截器,过滤json数据中的null值,将null变成空字符串,内含截图,及jar包
一个list里面有多个对象,对象有几个字段,要求在对象里面不要有重复的数据的实现。
关于讲解在java 中List使用方法
List对象排序通用方法List对象排序通用方法List对象排序通用方法List对象排序通用方法
以上这篇python 寻找list中最大元素对应的索引方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:python numpy和list查询其中某个数的个数及定位方法...
list.size()与list==null的区别 两者的区别是显而易见的,下面这两段就能显示两者差别: List list1 = new ArrayList(); System.out.println(list1.size());//0 List list2 = null; System.out.println(list2.size()...
jdk7中list的一些功能用起来比较繁琐,比如去重,得通过好几层for循环遍历判断得到,而jdk8 中lambda表达式提供了一些方便list操作的方法,主要涵盖分组、过滤、求和、最值、排序、去重。跟之前的传统写法对比,能少...
NULL 博文链接:https://bijian1013.iteye.com/blog/2358367
// GOF给出的定义为:提供一种方法访问一个容器(container)对象中的各个元素, // 而又不需暴露该对象的内部细节。 // 学习set对象容器的使用 // set容器中的对象不允许重复 // set容器接口的实现类有HashSet和 ...
在List中,通过方法去除其中存在相同的值
一、List对象中的T是值类型的情况(int 类型等)...1、对于引用类型的List无法用以上方法进行复制,只会复制List中对象的引用,可以用以下扩展方法复制: static class Extensions { public static IList<T> Clone(t
解决了某些系统中,ftpClient.listFiles()返回值总是为空的问题。 使用方法如下: ftpClient.changeWorkingDirectory(path); ftpClient.enterLocalPassiveMode(); ftpClient.configure(new FTPClientConfig(...
List的contains方法、Array的contains方法的使用
List类几种排序方法比较灵活 可以根据自己的需要 选用合适的方法
该资源在VS2008SP1 IDE中编写, 主要介绍了VC++中List Control控件的使用方法,包括:样式设置、列名设置、数据设置和双击响应操作等。将对你学会使用List Control控件产生巨大的帮助。 注意使用流程:在界面中新...
DataSet转化为List的通用方法,可自动转换类型
java程序设计中List的定义和方法,用于Java程序设计。
NULL 博文链接:https://xuedong.iteye.com/blog/1157186
list control 的使用方法,一学就会
Docker中的Alist挂载qBittorrent实现离线下载 1.Alistzh中设置qBittorrent的信息 1)进入Alist 2)选择“管理” 3)在设置中点“其他” 在下面的红色框中输入对应qBittorrent的用户名,密码,访问IP及端口号 输入...