From 57ea3a31a3f7786be88b8e8f89c71e9487358bb5 Mon Sep 17 00:00:00 2001 From: mcp Date: Tue, 17 Jan 2023 16:13:20 +0800 Subject: [PATCH] =?UTF-8?q?cn.hutool.core.collection.CollUtil.subtract=20?= =?UTF-8?q?=E9=92=88=E5=AF=B9=20coll1=20=E4=B8=BA=E5=8F=AA=E8=AF=BB?= =?UTF-8?q?=E9=9B=86=E5=90=88=E7=9A=84=E8=A1=A5=E5=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/collection/CollUtil.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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 fa94976ed..9f46e9254 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -365,11 +365,18 @@ public class CollUtil { */ public static Collection subtract(Collection coll1, Collection coll2) { Collection result = ObjectUtil.clone(coll1); - if (null == result) { - result = CollUtil.create(coll1.getClass()); + try { + if (null == result) { + result = CollUtil.create(coll1.getClass()); + result.addAll(coll1); + } + result.removeAll(coll2); + } catch (UnsupportedOperationException e){ + // 针对 coll1 为只读集合的补偿 + result = CollUtil.create(AbstractCollection.class); result.addAll(coll1); + result.removeAll(coll2); } - result.removeAll(coll2); return result; }