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 e6ef5f69d..a5cbfa0fa 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 @@ -667,6 +667,36 @@ public class CollUtil { return currentAlaDatas; } + /** + * 是否至少有一个符合判断条件 + * + * @param 集合元素类型 + * @param collection 集合 + * @param predicate 自定义判断函数 + * @return 是否有一个值匹配 布尔值 + */ + public static boolean anyMatch(Collection collection,Predicate predicate){ + if(isEmpty(collection)){ + return Boolean.FALSE; + } + return collection.stream().anyMatch(predicate); + } + + /** + * 是否全部匹配判断条件 + * + * @param 集合元素类型 + * @param collection 集合 + * @param predicate 自定义判断函数 + * @return 是否全部匹配 布尔值 + */ + public static boolean allMatch(Collection collection,Predicate predicate){ + if(isEmpty(collection)){ + return Boolean.FALSE; + } + return collection.stream().allMatch(predicate); + } + // ----------------------------------------------------------------------------------------------- new HashSet /** diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 8160a2a74..7eb0b85ab 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -389,9 +389,9 @@ public class CollUtilTest { @Test public void sortByPropertyTest() { final List list = CollUtil.newArrayList( - new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// + new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// ); CollUtil.sortByProperty(list, "createTime"); @@ -403,9 +403,9 @@ public class CollUtilTest { @Test public void sortByPropertyTest2() { final List list = CollUtil.newArrayList( - new TestBean("张三", 0, DateUtil.parse("2018-05-01")), // - new TestBean("李四", -12, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 23, DateUtil.parse("2018-04-01"))// + new TestBean("张三", 0, DateUtil.parse("2018-05-01")), // + new TestBean("李四", -12, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 23, DateUtil.parse("2018-04-01"))// ); CollUtil.sortByProperty(list, "age"); @@ -417,8 +417,8 @@ public class CollUtilTest { @Test public void fieldValueMapTest() { final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// ); final Map map = CollUtil.fieldValueMap(list, "name"); @@ -430,8 +430,8 @@ public class CollUtilTest { @Test public void fieldValueAsMapTest() { final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 14, DateUtil.parse("2018-04-01"))// + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 14, DateUtil.parse("2018-04-01"))// ); final Map map = CollUtil.fieldValueAsMap(list, "name", "age"); @@ -764,14 +764,14 @@ public class CollUtilTest { Assert.assertFalse(CollUtil.addIfAbsent(null, "123")); Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList("123"), "123")); Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Animal("jack", 20))); + new Animal("jack", 20))); // 正常情况 Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList("456"), "123")); Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Dog("jack", 20))); + new Dog("jack", 20))); Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Animal("tom", 20))); + new Animal("tom", 20))); } @Test @@ -782,9 +782,9 @@ public class CollUtilTest { oldMap.put("c", "134"); final Map map = CollUtil.toMap(oldMap.entrySet(), - new HashMap<>(), - Map.Entry::getKey, - entry -> Long.parseLong(entry.getValue())); + new HashMap<>(), + Map.Entry::getKey, + entry -> Long.parseLong(entry.getValue())); Assert.assertEquals(1L, (long) map.get("a")); Assert.assertEquals(12L, (long) map.get("b")); @@ -856,12 +856,12 @@ public class CollUtilTest { public void setValueByMapTest() { // https://gitee.com/dromara/hutool/pulls/482 final List people = Arrays.asList( - new Person("aa", 12, "man", 1), - new Person("bb", 13, "woman", 2), - new Person("cc", 14, "man", 3), - new Person("dd", 15, "woman", 4), - new Person("ee", 16, "woman", 5), - new Person("ff", 17, "man", 6) + new Person("aa", 12, "man", 1), + new Person("bb", 13, "woman", 2), + new Person("cc", 14, "man", 3), + new Person("dd", 15, "woman", 4), + new Person("ee", 16, "woman", 5), + new Person("ff", 17, "man", 6) ); final Map genderMap = new HashMap<>(); @@ -902,12 +902,12 @@ public class CollUtilTest { @Test public void distinctByFunctionTest() { final List people = Arrays.asList( - new Person("aa", 12, "man", 1), - new Person("bb", 13, "woman", 2), - new Person("cc", 14, "man", 3), - new Person("dd", 15, "woman", 4), - new Person("ee", 16, "woman", 5), - new Person("ff", 17, "man", 6) + new Person("aa", 12, "man", 1), + new Person("bb", 13, "woman", 2), + new Person("cc", 14, "man", 3), + new Person("dd", 15, "woman", 4), + new Person("ee", 16, "woman", 5), + new Person("ff", 17, "man", 6) ); // 覆盖模式下ff覆盖了aa,ee覆盖了bb @@ -964,8 +964,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3, 4, 5, 6).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3, 4, 5, 6).toArray(), + list.toArray()); } @Test @@ -975,8 +975,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @Test @@ -986,8 +986,8 @@ public class CollUtilTest { @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List list = CollUtil.unionAll(list1, list2, null); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @Test @@ -997,8 +997,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, null, null); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @SuppressWarnings("ConstantValue") @@ -1057,4 +1057,13 @@ public class CollUtilTest { final Object first = CollUtil.getFirst(nullList); Assert.assertNull(first); } + + @Test + public void testMatch() { + List 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)); + } }