在使用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. loadrunner11--基础使用

    每次开启电脑都需要破解一次Lr,汉化版的有问题,建议使用英文版的.我测试的环境是Windows7+IE8+LR11.(在Windows10上试过,谷歌和IE11都不能正常运行),以下我会具体来操作,最 ...

  2. nginx 根据get参数重定向(根据电视访问的mac地址传递的值,来重定向访问别的url地址,这样就可以进行单台的测试环境。。)

    背景是这样的: 公司要做所有客户端的迁移到别的云平台,但又担心会有问题,所以考虑分批次迁移过去,这样就需要迁移部分用户,因为客户端刷但都是统一但rom包,不能轻易发生改动,所以决定用重定向方式将部分客 ...

  3. Beta周王者荣耀交流协会第六次会议

    1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:袁玥 2. 时间跨度 2017年11月15日 19:00 — 19:10 ,总计10分钟. 3. 地点 一食堂 ...

  4. YQCB冲刺周第一天

    团队讨论的照片 任务看板为 今天小组成员讨论了每个页面的排版,每个页面的跳转,以及页面的排版. 今天准备编写登录界面的.注册界面的代码. 遇到的困难是用户记账时选择的分类标准很多,最后将其整合,删减.

  5. c# dictionnary根据value查找对应的key

    属性方法中并没有包含此功能,因此需要自己自定义一个方法: string regionName = ""; if (ControlForm.swichLanguage.Contain ...

  6. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛

    Supreme Number 1000ms 131072K   A prime number (or a prime) is a natural number greater than 111 tha ...

  8. 关于window.open弹出窗口被阻止的问题

    原文:http://blog.csdn.net/fanfanjin/article/details/6858168 在web编程过程中,经常会遇到一些页面需要弹出窗口,但是在服务器端用window.o ...

  9. 0422 寻找数学口袋精灵BUG

    首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK      http://www.lvtao.net/server/windows-setup-jdk.h ...

  10. 201621123037 《Java程序设计》第12周学习总结

    作业12-流与文件 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 答: 读取操作 从文件中读取: 1.字节流 InputStr ...