add method

This commit is contained in:
Looly 2021-12-22 00:28:29 +08:00
parent 1279f9f6ab
commit 6eb5a76f80
3 changed files with 64 additions and 8 deletions

View File

@ -13,6 +13,7 @@
* 【core 】 Img.scale缩小默认使用平滑模式增加scale方法重载可选模式issue#I4MY6X@Gitee * 【core 】 Img.scale缩小默认使用平滑模式增加scale方法重载可选模式issue#I4MY6X@Gitee
* 【core 】 excel添加写入图片的方法pr#486@Gitee * 【core 】 excel添加写入图片的方法pr#486@Gitee
* 【core 】 增加CollStreamUtil.groupBypr#484@Gitee * 【core 】 增加CollStreamUtil.groupBypr#484@Gitee
* 【core 】 增加CollUtil.setValueByMappr#482@Gitee
* *
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 LineReadWatcher#onModify文件清空判断问题issue#2013@Github * 【core 】 LineReadWatcher#onModify文件清空判断问题issue#2013@Github

View File

@ -1301,7 +1301,7 @@ public class CollUtil {
* @param <E> 集合元素类型 * @param <E> 集合元素类型
* @param resultCollection 存放移除结果的集合 * @param resultCollection 存放移除结果的集合
* @param targetCollection 被操作移除元素的集合 * @param targetCollection 被操作移除元素的集合
* @param predicate 用于是否移除判断的过滤器 * @param predicate 用于是否移除判断的过滤器
*/ */
public static <T extends Collection<E>, E> T removeWithAddIf(T targetCollection, T resultCollection, Predicate<? super E> predicate) { public static <T extends Collection<E>, E> T removeWithAddIf(T targetCollection, T resultCollection, Predicate<? super E> predicate) {
Objects.requireNonNull(predicate); Objects.requireNonNull(predicate);
@ -2931,14 +2931,19 @@ public class CollUtil {
/** /**
* 使用给定的map将集合中的原素进行属性或者值的重新设定 * 使用给定的map将集合中的原素进行属性或者值的重新设定
* @param collection 集合 *
* @param map 映射集 * @param <E> 元素类型
* @param keyGenerate 映射键生成函数 * @param <K> 替换的键
* @param biConsumer 封装映射到的值函数 * @param <V> 替换的值
* @param iterable 集合
* @param map 映射集
* @param keyGenerate 映射键生成函数
* @param biConsumer 封装映射到的值函数
* @author nick_wys * @author nick_wys
* @since 5.7.18
*/ */
public static <E, K, V> void setValueByMap(Collection<E> collection, Map<K, V> map, Function<E, K> keyGenerate, BiConsumer<E, V> biConsumer) { public static <E, K, V> void setValueByMap(Iterable<E> iterable, Map<K, V> map, Function<E, K> keyGenerate, BiConsumer<E, V> biConsumer) {
collection.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y))); iterable.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y)));
} }
// ---------------------------------------------------------------------------------------------- Interface start // ---------------------------------------------------------------------------------------------- Interface start

View File

@ -827,4 +827,54 @@ public class CollUtilTest {
final List<String> sort = CollUtil.sort(of, new ComparableComparator<>()); final List<String> sort = CollUtil.sort(of, new ComparableComparator<>());
Assert.assertEquals("a,b,c", CollUtil.join(sort, ",")); Assert.assertEquals("a,b,c", CollUtil.join(sort, ","));
} }
@Test
public void setValueByMapTest(){
// https://gitee.com/dromara/hutool/pulls/482
List<Person> 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<Integer, String> 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<Integer, Person> 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;
}
} }