diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java
index 010a28c80..5c4f4358d 100644
--- a/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java
+++ b/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java
@@ -12,6 +12,7 @@
package org.dromara.hutool.core.collection;
+import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.codec.hash.Hash32;
import org.dromara.hutool.core.collection.iter.IterUtil;
@@ -25,9 +26,9 @@ import org.dromara.hutool.core.comparator.PropertyComparator;
import org.dromara.hutool.core.convert.CompositeConverter;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.exception.HutoolException;
-import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.func.SerBiConsumer;
import org.dromara.hutool.core.func.SerConsumer3;
+import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.reflect.FieldUtil;
@@ -35,7 +36,6 @@ import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.core.stream.StreamUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil;
-import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.lang.reflect.Type;
@@ -51,7 +51,6 @@ import java.util.stream.Collectors;
* 集合相关工具类
*
* 此工具方法针对{@link Collection}或{@link Iterable}及其实现类封装的工具。
- *
*
* @author Looly
* @see IterUtil
@@ -1070,12 +1069,12 @@ public class CollUtil {
public static List map(final Iterable collection, final Function super T, ? extends R> mapper, final boolean ignoreNull) {
if (ignoreNull) {
return StreamUtil.of(collection)
- // 检查映射前的结果
- .filter(Objects::nonNull)
- .map(mapper)
- // 检查映射后的结果
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
+ // 检查映射前的结果
+ .filter(Objects::nonNull)
+ .map(mapper)
+ // 检查映射后的结果
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
}
return StreamUtil.of(collection).map(mapper).collect(Collectors.toList());
}
@@ -1317,7 +1316,7 @@ public class CollUtil {
*
* @param 元素类型
* @param collection 集合
- * @param predicate 匹配器,为空则全部匹配
+ * @param predicate 匹配器,为空则全部匹配
* @return 位置数组
*/
public static List indexListOfAll(final Collection collection, final Predicate predicate) {
@@ -2321,4 +2320,33 @@ public class CollUtil {
return collection.add(object);
}
+ /**
+ * 是否至少有一个符合判断条件
+ *
+ * @param 集合元素类型
+ * @param collection 集合
+ * @param predicate 自定义判断函数
+ * @return 是否有一个值匹配 布尔值
+ */
+ public static boolean anyMatch(final Collection collection, final Predicate predicate) {
+ if (isEmpty(collection)) {
+ return Boolean.FALSE;
+ }
+ return collection.stream().anyMatch(predicate);
+ }
+
+ /**
+ * 是否全部匹配判断条件
+ *
+ * @param 集合元素类型
+ * @param collection 集合
+ * @param predicate 自定义判断函数
+ * @return 是否全部匹配 布尔值
+ */
+ public static boolean allMatch(final Collection collection, final Predicate predicate) {
+ if (isEmpty(collection)) {
+ return Boolean.FALSE;
+ }
+ return collection.stream().allMatch(predicate);
+ }
}
diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/collection/CollUtilTest.java
index 8ef881190..e177e8e0d 100644
--- a/hutool-core/src/test/java/org/dromara/hutool/core/collection/CollUtilTest.java
+++ b/hutool-core/src/test/java/org/dromara/hutool/core/collection/CollUtilTest.java
@@ -137,6 +137,7 @@ public class CollUtilTest {
Assertions.assertEquals(srcList, answerList);
}
+ @SuppressWarnings("ConstantValue")
@Test
public void isNotEmptyTest() {
Assertions.assertFalse(CollUtil.isNotEmpty((Collection>) null));
@@ -505,9 +506,9 @@ public class CollUtilTest {
);
final Map map = CollUtil.fieldValueAsMap(list, "name", "age");
- Assertions.assertEquals(new Integer(12), map.get("张三"));
- Assertions.assertEquals(new Integer(13), map.get("李四"));
- Assertions.assertEquals(new Integer(14), map.get("王五"));
+ Assertions.assertEquals(Integer.valueOf(12), map.get("张三"));
+ Assertions.assertEquals(Integer.valueOf(13), map.get("李四"));
+ Assertions.assertEquals(Integer.valueOf(14), map.get("王五"));
}
@Test
@@ -1153,4 +1154,13 @@ public class CollUtilTest {
Assertions.assertFalse(CollUtil.isEqualList(list, ListUtil.of(1, 2, 3)));
Assertions.assertFalse(CollUtil.isEqualList(list, ListUtil.of(4, 3, 2, 1)));
}
+
+ @Test
+ public void testMatch() {
+ final List list = Arrays.asList(1, 2, 3, 4, 5, 6);
+ Assertions.assertTrue(CollUtil.anyMatch(list, i -> i == 1));
+ Assertions.assertFalse(CollUtil.anyMatch(list, i -> i > 6));
+ Assertions.assertFalse(CollUtil.allMatch(list, i -> i == 1));
+ Assertions.assertTrue(CollUtil.allMatch(list, i -> i <= 6));
+ }
}