mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
1279f9f6ab
commit
6eb5a76f80
@ -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)
|
||||
|
@ -2931,14 +2931,19 @@ public class CollUtil {
|
||||
|
||||
/**
|
||||
* 使用给定的map将集合中的原素进行属性或者值的重新设定
|
||||
* @param collection 集合
|
||||
*
|
||||
* @param <E> 元素类型
|
||||
* @param <K> 替换的键
|
||||
* @param <V> 替换的值
|
||||
* @param iterable 集合
|
||||
* @param map 映射集
|
||||
* @param keyGenerate 映射键生成函数
|
||||
* @param biConsumer 封装映射到的值函数
|
||||
* @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) {
|
||||
collection.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y)));
|
||||
public static <E, K, V> void setValueByMap(Iterable<E> iterable, Map<K, V> map, Function<E, K> keyGenerate, BiConsumer<E, V> biConsumer) {
|
||||
iterable.forEach(x -> Optional.ofNullable(map.get(keyGenerate.apply(x))).ifPresent(y -> biConsumer.accept(x, y)));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- Interface start
|
||||
|
@ -827,4 +827,54 @@ public class CollUtilTest {
|
||||
final List<String> 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<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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user