CollectionUtils.isEqualCollection的用法
在使用Java的集合时,有些时候会需要比较两个集合是否相等,自己写方法其实也简单,但是既然有了好的实现,就不要自己造轮子了,只要了解这个轮子是什么原理就好了。
1 | public static boolean isEqualCollection( final Collection<?> a, final Collection<?> b) |
传入两个Collection就可以了,我们常用的List或者Set,根据源码发现:
这个方法比较的是集合中的元素以及元素的个数,不管是List或者是Set,不要求顺序相同。
下面是一个例子,其中的源码可能因为版本更迭与最新的不同,但是原理是一样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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 ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~