diff --git a/CHANGELOG.md b/CHANGELOG.md index 9decc9000..4c888ca19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 新特性 * 【poi 】 重要:不再兼容POI-3.x,增加兼容POI-5.x(issue#I35J6B@Gitee) * 【core 】 FileTypeUtil使用长匹配优先(pr#1457@Github) +* 【core 】 IterUtil和CollUtil增加isEqualList方法(issue#I3A3PY@Gitee) ### Bug修复 * 【socket 】 修复Client创建失败资源未释放问题。 diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 9c8819f22..927ecb69e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -2988,4 +2988,25 @@ public class CollUtil { } return total; } + + /** + * 判断两个{@link Collection} 是否元素和顺序相同,返回{@code true}的条件是: + *
- * Adapt the specified Enumeration to the Iterator interface
+ * Adapt the specified {@code Enumeration} to the {@code Iterator} interface
*
* @param
+ *
+ * 此方法来自Apache-Commons-Collections4。
+ *
+ * @param list1 列表1
+ * @param list2 列表2
+ * @return 是否相同
+ * @since 5.6.0
+ */
+ public static boolean isEqualList(final Iterable> list1, final Iterable> list2) {
+ if (list1 == list2) {
+ return true;
+ }
+
+ final Iterator> it1 = list1.iterator();
+ final Iterator> it2 = list2.iterator();
+ Object obj1;
+ Object obj2;
+ while (it1.hasNext() && it2.hasNext()) {
+ obj1 = it1.next();
+ obj2 = it2.next();
+
+ if (false == Objects.equals(obj1, obj2)) {
+ return false;
+ }
+ }
+
+ // 当两个Iterable长度不一致时返回false
+ return false == (it1.hasNext() || it2.hasNext());
+ }
}