This commit is contained in:
Looly 2023-06-03 01:40:59 +08:00
parent c4420ae52a
commit 1765e82ef5
2 changed files with 51 additions and 13 deletions

View File

@ -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;
* 集合相关工具类
* <p>
* 此工具方法针对{@link Collection}{@link Iterable}及其实现类封装的工具
* <p>
*
* @author Looly
* @see IterUtil
@ -1070,12 +1069,12 @@ public class CollUtil {
public static <T, R> List<R> map(final Iterable<T> 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 <T> 元素类型
* @param collection 集合
* @param predicate 匹配器为空则全部匹配
* @param predicate 匹配器为空则全部匹配
* @return 位置数组
*/
public static <T> List<Integer> indexListOfAll(final Collection<T> collection, final Predicate<T> predicate) {
@ -2321,4 +2320,33 @@ public class CollUtil {
return collection.add(object);
}
/**
* 是否至少有一个符合判断条件
*
* @param <T> 集合元素类型
* @param collection 集合
* @param predicate 自定义判断函数
* @return 是否有一个值匹配 布尔值
*/
public static <T> boolean anyMatch(final Collection<T> collection, final Predicate<T> predicate) {
if (isEmpty(collection)) {
return Boolean.FALSE;
}
return collection.stream().anyMatch(predicate);
}
/**
* 是否全部匹配判断条件
*
* @param <T> 集合元素类型
* @param collection 集合
* @param predicate 自定义判断函数
* @return 是否全部匹配 布尔值
*/
public static <T> boolean allMatch(final Collection<T> collection, final Predicate<T> predicate) {
if (isEmpty(collection)) {
return Boolean.FALSE;
}
return collection.stream().allMatch(predicate);
}
}

View File

@ -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<String, Integer> 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<Integer> 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));
}
}