From 8e5ed2b69118682563da9541636b72007e60b4c4 Mon Sep 17 00:00:00 2001 From: "hbn.king" Date: Tue, 2 Nov 2021 10:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E6=B3=A8=E9=87=8A=20?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/CollUtil.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 94e992aa1..fa4233bd4 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 @@ -414,12 +414,31 @@ public class CollUtil { * @param collection 集合 * @param value 需要查找的值 * @return 如果集合为空(null或者空),返回{@code false},否则找到元素返回{@code true} + * @throws ClassCastException 如果类型不一致会抛出转换异常 + * @throws NullPointerException 当指定的元素 值为 null ,或集合类不支持null 时抛出该异常 + * @see Collection#contains(Object) * @since 4.1.10 */ public static boolean contains(Collection collection, Object value) { return isNotEmpty(collection) && collection.contains(value); } + /** + * 判断指定集合是否包含指定值,如果集合为空(null或者空),返回{@code false},否则找到元素返回{@code true} + * @param collection 集合 + * @param value 需要查找的值 + * @return 果集合为空(null或者空),返回{@code false},否则找到元素返回{@code true} + */ + public static boolean safeContains(Collection collection, Object value) { + + try { + return contains(collection ,value); + } catch (ClassCastException | NullPointerException e) { + return false; + } + } + + /** * 自定义函数判断集合是否包含某类值 *