在使用Java的集合时,有些时候会需要比较两个集合是否相等,自己写方法其实也简单,但是既然有了好的实现,就不要自己造轮子了,只要了解这个轮子是什么原理就好了。

public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b)

传入两个Collection就可以了,我们常用的List或者Set,根据源码发现:

这个方法比较的是集合中的元素以及元素的个数,不管是List或者是Set,不要求顺序相同。

下面是一个例子,其中的源码可能因为版本更迭与最新的不同,但是原理是一样的:

package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test; public class Test1 { // CollectionUtils.isEqualCollection
/**
* isEqualCollection
*
* public static <E> boolean isEqualCollection(Collection<E> a, Collection<E> b) Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.
* That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
*
* Parameters:
* a - the first collection, must not be null
* b - the second collection, must not be null
* Returns:
* true iff the collections contain the same elements with the same cardinalities.
*
*/
@Test
public void test1() {
ArrayList<String> arr1 = new ArrayList<String>();
arr1.add("1");
arr1.add("2");
arr1.add("2");
arr1.add("3");
arr1.add("4");
arr1.add("5");
ArrayList<String> arr2 = new ArrayList<String>();
arr2.add("1");
arr2.add("1");
arr2.add("2");
arr2.add("3");
arr2.add("5");
arr2.add("4");
System.out.println(CollectionUtils.isEqualCollection(arr1, arr2)); // copy的源码
System.out.println(isEqualCollection(arr1, arr2));
} public static boolean isEqualCollection(Collection a, Collection b) {
if (a.size() != b.size())
return false;
Map mapa = getCardinalityMap(a);
Map mapb = getCardinalityMap(b);
if (mapa.size() != mapb.size())
return false;
for (Iterator it = mapa.keySet().iterator(); it.hasNext();) {
Object obj = it.next();
if (getFreq(obj, mapa) != getFreq(obj, mapb))
return false;
} return true;
} private static Integer INTEGER_ONE = new Integer(1); public static Map getCardinalityMap(Collection coll) {
Map count = new HashMap();
for (Iterator it = coll.iterator(); it.hasNext();) {
Object obj = it.next();
Integer c = (Integer) count.get(obj);
if (c == null)
count.put(obj, INTEGER_ONE);
else
count.put(obj, new Integer(c.intValue() + 1));
} return count;
} private static final int getFreq(Object obj, Map freqMap) {
Integer count = (Integer) freqMap.get(obj);
if (count != null)
return count.intValue();
else
return 0;
}
}

CollectionUtils.isEqualCollection的用法的更多相关文章

  1. CollectionUtils工具类的常用方法

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

  2. CollectionUtils工具类使用指南

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

  3. java判断集合是否相等

    1,使用commons-collection-3.2.1.jar包中的CollectionUtils.isEqualCollection()方法 2,还有其他集合操作:disjunction(a,b集 ...

  4. SpEL 实例

    SpEl 实例 基于 Spring 解析 @RestController @RequestMapping("/spel") @Slf4j public class SpELCont ...

  5. Java中判断两个列表是否相等

    CollectionUtils.isEqualCollection(final Collection a, final Collection b) CollectionUtils工具类中有一个查看两个 ...

  6. nacos集群

    本章分析一下nacos集群之间nacos服务器上线,下线原理 每5秒运行定时任务ServerListManager.ServerListUpdater获取新上线的节点或下线的节点 每2秒运行定时任务S ...

  7. BeanUtils JavaBean 工具包使用

    感谢原文作者:小老弟 原文链接:https://www.cnblogs.com/syncmr/p/10523576.html 目录 简介 BeanUtils类 使用示例 ConvertUtils 功能 ...

  8. CollectionUtils.select用法

    import java.util.ArrayList;import java.util.List; import org.apache.commons.collections.CollectionUt ...

  9. SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法

    1.SpringMVC中通过@ResponseBody.@RequestParam默认值,@PathVariable的用法 package com.kuman.cartoon.controller.f ...

随机推荐

  1. 17 Tips For Writing An Excellent Email Subject Line

    Out of the billions of emails that are sent every day, how can you make sure that yours stands out? ...

  2. Python模块random使用详情

    python常用模块目录 1.random.random()#用于生成一个0到1的随机浮点数:0<= n < 1.0 import random mcw = random.random() ...

  3. 第五次ScrumMeeting博客

    第五次ScrumMeeting博客 本次会议于10月29日(日)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕. 1. 每个人的工作(有Issue的内容和 ...

  4. 用了这么多年的MCU,你知道哪些MCU原厂最牛?

    单片机诞生于1971年,经历了SCM.MCU.SoC三大阶段.单片机由以前的1位.4位.8位.16位,发展到现在的32位甚至64位. 90年代后随着消费电子产品大发展,单片机技术得到了巨大提高,相继诞 ...

  5. 《JavaScript》web客户端存储

    Web存储: 兼容IE8在内的所有主流浏览器,不兼容早期浏览器:支持大容量但非无限量 LocalStorage和sessionStorage是window对象的两个属性,这两个属性都代表同一个stor ...

  6. Python:字典操作总结

     字典是Python中唯一的映射类型 [注]:字典中数据是无序排放的 一.字典的创建方法 方法1:用大括号包裹键值对从而创建字典 addict={}#创建一个空字典 addict={key1:valu ...

  7. Android开发第二阶段(2)

    昨天:总结了第一阶段的开发经验 今天:学习了一下java中对事件处理这块的初步了解比如设置监听器等 明天:我会走进我们的代码去看看相关的一些知识.

  8. a5

    今日内容: 今天主要还是素材的查找,图标的制作以及调整. 明日计划: 主要还是完成图标,尽可能的美化 困难: 一个是直男式的审美吧,另一个是PS的技术还不够深

  9. 3dContactPointAnnotationTool开发日志(三二)

      今天就是看怎么把论文的python源码预测出来的smpl模型的姿势和形状参数弄到unity版本的smpl里,但是python版本的和unity版本的不一样.   先看看他的fit_3d.py:   ...

  10. 在Delphi中动态地使用SQL查询语句 Adoquery sql 参数 冒号

    在Delphi中动态地使用SQL查询语句 在一般的数据库管理系统中,通常都需要应用SQL查询语句来提高程序的动态特性.下面介绍如何在Delphi中实现这种功能.在Delphi中,使用SQL查询语句的途 ...