mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
CollUtil新增allMatch和anyMatch方法
This commit is contained in:
parent
1ead9573b2
commit
003b221f38
@ -667,6 +667,36 @@ public class CollUtil {
|
|||||||
return currentAlaDatas;
|
return currentAlaDatas;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否至少有一个符合判断条件
|
||||||
|
*
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @param collection 集合
|
||||||
|
* @param predicate 自定义判断函数
|
||||||
|
* @return 是否有一个值匹配 布尔值
|
||||||
|
*/
|
||||||
|
public static <T>boolean anyMatch(Collection<T> collection,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(Collection<T> collection,Predicate<T> predicate){
|
||||||
|
if(isEmpty(collection)){
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
return collection.stream().allMatch(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------- new HashSet
|
// ----------------------------------------------------------------------------------------------- new HashSet
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1057,4 +1057,13 @@ public class CollUtilTest {
|
|||||||
final Object first = CollUtil.getFirst(nullList);
|
final Object first = CollUtil.getFirst(nullList);
|
||||||
Assert.assertNull(first);
|
Assert.assertNull(first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMatch() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||||
|
Assert.assertTrue(CollUtil.anyMatch(list, i -> i == 1));
|
||||||
|
Assert.assertFalse(CollUtil.anyMatch(list, i -> i > 6));
|
||||||
|
Assert.assertFalse(CollUtil.allMatch(list, i -> i == 1));
|
||||||
|
Assert.assertTrue(CollUtil.allMatch(list, i -> i <= 6));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user