diff --git a/CHANGELOG.md b/CHANGELOG.md index 0187dde38..85372de40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【core 】 Img.scale缩小默认使用平滑模式,增加scale方法重载可选模式(issue#I4MY6X@Gitee) * 【core 】 excel添加写入图片的方法(pr#486@Gitee) * 【core 】 增加CollStreamUtil.groupBy(pr#484@Gitee) +* 【core 】 增加CollUtil.setValueByMap(pr#482@Gitee) * ### 🐞Bug修复 * 【core 】 LineReadWatcher#onModify文件清空判断问题(issue#2013@Github) 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 05189b4b0..358a8e2e9 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 @@ -1301,7 +1301,7 @@ public class CollUtil { * @param 集合元素类型 * @param resultCollection 存放移除结果的集合 * @param targetCollection 被操作移除元素的集合 - * @param predicate 用于是否移除判断的过滤器 + * @param predicate 用于是否移除判断的过滤器 */ public static , E> T removeWithAddIf(T targetCollection, T resultCollection, Predicate predicate) { Objects.requireNonNull(predicate); @@ -2928,17 +2928,22 @@ public class CollUtil { public static Collection trans(Collection collection, Function function) { return new TransCollection<>(collection, function); } - + /** * 使用给定的map将集合中的原素进行属性或者值的重新设定 - * @param collection 集合 - * @param map 映射集 - * @param keyGenerate 映射键生成函数 - * @param biConsumer 封装映射到的值函数 + * + * @param 元素类型 + * @param 替换的键 + * @param 替换的值 + * @param iterable 集合 + * @param map 映射集 + * @param keyGenerate 映射键生成函数 + * @param biConsumer 封装映射到的值函数 * @author nick_wys + * @since 5.7.18 */ - public static void setValueByMap(Collection collection, Map map, Function keyGenerate, BiConsumer biConsumer) { - collection.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y))); + public static void setValueByMap(Iterable iterable, Map map, Function keyGenerate, BiConsumer biConsumer) { + iterable.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y))); } // ---------------------------------------------------------------------------------------------- Interface start 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 bd889916f..4e866979d 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -827,4 +827,54 @@ public class CollUtilTest { final List sort = CollUtil.sort(of, new ComparableComparator<>()); Assert.assertEquals("a,b,c", CollUtil.join(sort, ",")); } + + @Test + public void setValueByMapTest(){ + // https://gitee.com/dromara/hutool/pulls/482 + 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) + ); + + Map genderMap = new HashMap<>(); + genderMap.put(1, null); + genderMap.put(2, "妇女"); + genderMap.put(3, "少女"); + genderMap.put(4, "女"); + genderMap.put(5, "小孩"); + genderMap.put(6, "男"); + + Assert.assertEquals(people.get(1).getGender(), "woman"); + CollUtil.setValueByMap(people, genderMap, Person::getId, Person::setGender); + Assert.assertEquals(people.get(1).getGender(), "妇女"); + + Map personMap = new HashMap<>(); + personMap.put(1, new Person("AA", 21, "男", 1)); + personMap.put(2, new Person("BB", 7, "小孩", 2)); + personMap.put(3, new Person("CC", 65, "老人", 3)); + personMap.put(4, new Person("DD", 35, "女人", 4)); + personMap.put(5, new Person("EE", 14, "少女", 5)); + personMap.put(6, null); + + CollUtil.setValueByMap(people, personMap, Person::getId, (x, y) -> { + x.setGender(y.getGender()); + x.setName(y.getName()); + x.setAge(y.getAge()); + }); + + Assert.assertEquals(people.get(1).getGender(), "小孩"); + } + + @Data + @AllArgsConstructor + static class Person { + private String name; + private Integer age; + private String gender; + private Integer id; + } }