Java集合中移除所有的null值
2022/6/9 1:20:08
本文主要是介绍Java集合中移除所有的null值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java集合中移除所有的null值
https://blog.csdn.net/qq_36135928/article/details/86605104
org.apache.commons.collections.subtract方法只能移除第一个null元素。
public class CollectionRemoveNullTest { @Test public void test() { List<String> nullList = new ArrayList<>(); nullList.add("a"); nullList.add(null); nullList.add("b"); nullList.add("c"); nullList.add(null); Collection<String> removeNull = subtract(nullList, Arrays.asList((String) null)); Iterator<String> it = removeNull.iterator(); while (it.hasNext()) { String s = it.next(); assertTrue(s != null); } } /** Returns a new {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>. The cardinality of each element <i>e</i> in the returned {@link Collection} will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality of <i>e</i> in <i>b</i>, or zero, whichever is greater. @param a the collection to subtract from, must not be null @param b the collection to subtract, must not be null @return a new collection with the results @see Collection#removeAll */ public static Collection subtract(final Collection a, final Collection b) { ArrayList list = new ArrayList(a); for (Iterator it = b.iterator(); it.hasNext()
这篇关于Java集合中移除所有的null值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?