CollectionUtils工具类

这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全。

使用前需导入依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>

API常用方法

        /**
* 1、除非元素为null,否则向集合添加元素
*/
CollectionUtils.addIgnoreNull(personList,null);
/**
* 2、将两个已排序的集合a和b合并为一个已排序的列表,以便保留元素的自然顺序
*/
CollectionUtils.collate(Iterable<? extends O> a, Iterable<? extends O> b)
/**
* 3、将两个已排序的集合a和b合并到一个已排序的列表中,以便保留根据Comparator c的元素顺序。
*/
CollectionUtils.collate(Iterable<? extends O> a, Iterable<? extends O> b, Comparator<? super O> c)
/**
* 4、返回该个集合中是否含有至少有一个元素
*/
CollectionUtils.containsAny(Collection<?> coll1, T... coll2)
/**
* 5、如果参数是null,则返回不可变的空集合,否则返回参数本身。(很实用 ,最终返回List EMPTY_LIST = new EmptyList<>())
*/
CollectionUtils.emptyIfNull(Collection<T> collection)
/**
* 6、空安全检查指定的集合是否为空
*/
CollectionUtils.isEmpty(Collection<?> coll)
/**
* 7、 空安全检查指定的集合是否为空。
*/
CollectionUtils.isNotEmpty(Collection<?> coll)
/**
* 8、反转给定数组的顺序。
*/
CollectionUtils.reverseArray(Object[] array);
/**
* 9、差集
*/
CollectionUtils.subtract(Iterable<? extends O> a, Iterable<? extends O> b)
/**
* 10、并集
*/
CollectionUtils.union(Iterable<? extends O> a, Iterable<? extends O> b)
/**
* 11、交集
*/
CollectionUtils.intersection(Collection a, Collection b)
/**
*12、 交集的补集(析取)
*/
CollectionUtils.disjunction(Collection a, Collection b)

CollectionUtils工具类的更多相关文章

  1. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  2. StringUtils、CollectionUtils工具类的常用方法

    唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...

  3. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  4. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  5. CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)

    一.CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2) 采用的类: import org.apache.commons.collec ...

  6. CollectionUtils工具类的常用方法

    集合判断:  例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...

  7. CollectionUtils工具类中常用方法

    @SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...

  8. CollectionUtils工具类使用指南

    CollectionUtils提供很多对集合的操作方法,常用的方法如下:(参考文章:http://www.open-open.com/code/view/1420470842125) import o ...

  9. 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils的使用

    主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度为0.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...

随机推荐

  1. centos6.9防火墙设置

    1.输入:cat /etc/issue   查看版本 2. service命令开启以及关闭防火墙为即时生效,下次重启机器的时候会自动复原. 查看防火墙状态:service iptables statu ...

  2. DateTimePicket jQuery 日期插件,开始时间和结束时间示例

    需要引入的js文件: <input type="text" id="startTime" placeholder="开始时间"/> ...

  3. okhttp 拦截问题

    builder.addInterceptor(chain -> { Request request = chain.request(); Response response = chain.pr ...

  4. Windows下 webstorm安装tomcat配置svn并使用

    先附上所需要的软件的下载地址:https://pan.baidu.com/s/1c2ripd2 1.下载并安装jdk以及配置jdk的环境变量 1)下载jdk,选择安装目录安装,我选择的是默认路径,安装 ...

  5. hibernate多表查询sql,以及所得对象的处理

    String sql ="SELECT id FROM tea WHERE tea.name=? "; SQLQuery query = this.getSession().cre ...

  6. [LC] 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  7. getHibernateTemplate()的find用法大全

    一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...

  8. Qt QString与int的转换

    QString转int QString a=" ; int b; b=a.toInt();   int 转 QString ; QString b; b=QString::number(a)

  9. Mac使用sublime text3的快捷键

    符号说明⌘:command⌃:control⌥:option⇧:shift↩:enter⌫:delete 打开/关闭/前往⌘⇧N 打开一个新的sublime窗口⌘N 新建文件⌘⇧W 关闭sublime ...

  10. 对Design model的理解与Java design model的归纳

    设计模式的起源是面向对象程序设计思想,是面向对象设计的精髓--抽象.面向对象通过类和对象来实现抽象,实现时产生了面向对象的三个重要机制:封装.继承.多态.正是这三个机制衍生出了各种各样的设计模式.在面 ...