mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复MapUtil工具使用filter方法构造传入参数结果问题
This commit is contained in:
parent
65fe2e7529
commit
7ab95e5cb8
@ -2,12 +2,13 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.21(2023-06-24)
|
# 5.8.21(2023-06-25)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 list 为空时,CollUtil.max等返回null而非异常(pr#1027@Gitee)
|
* 【core 】 list 为空时,CollUtil.max等返回null而非异常(pr#1027@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题(issue#3162@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.20(2023-06-16)
|
# 5.8.20(2023-06-16)
|
||||||
|
@ -3,10 +3,7 @@ package cn.hutool.core.map;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Editor;
|
import cn.hutool.core.lang.*;
|
||||||
import cn.hutool.core.lang.Filter;
|
|
||||||
import cn.hutool.core.lang.Pair;
|
|
||||||
import cn.hutool.core.lang.TypeReference;
|
|
||||||
import cn.hutool.core.stream.CollectorUtil;
|
import cn.hutool.core.stream.CollectorUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.JdkUtil;
|
import cn.hutool.core.util.JdkUtil;
|
||||||
@ -687,6 +684,11 @@ public class MapUtil {
|
|||||||
return map2;
|
return map2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issue#3162@Github,在构造中put值,会导致新建map带有值内容,此处清空
|
||||||
|
if(false == map2.isEmpty()){
|
||||||
|
map2.clear();
|
||||||
|
}
|
||||||
|
|
||||||
Entry<K, V> modified;
|
Entry<K, V> modified;
|
||||||
for (Entry<K, V> entry : map.entrySet()) {
|
for (Entry<K, V> entry : map.entrySet()) {
|
||||||
modified = editor.edit(entry);
|
modified = editor.edit(entry);
|
||||||
@ -763,6 +765,11 @@ public class MapUtil {
|
|||||||
return map2;
|
return map2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issue#3162@Github,在构造中put值,会导致新建map带有值内容,此处清空
|
||||||
|
if(false == map2.isEmpty()){
|
||||||
|
map2.clear();
|
||||||
|
}
|
||||||
|
|
||||||
for (K key : keys) {
|
for (K key : keys) {
|
||||||
if (map.containsKey(key)) {
|
if (map.containsKey(key)) {
|
||||||
map2.put(key, map.get(key));
|
map2.put(key, map.get(key));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.map;
|
package cn.hutool.core.map;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.lang.Dict;
|
import cn.hutool.core.lang.Dict;
|
||||||
import cn.hutool.core.lang.Opt;
|
import cn.hutool.core.lang.Opt;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -47,13 +48,13 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterTest() {
|
public void filterTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap();
|
final Map<String, String> map = MapUtil.newHashMap();
|
||||||
map.put("a", "1");
|
map.put("a", "1");
|
||||||
map.put("b", "2");
|
map.put("b", "2");
|
||||||
map.put("c", "3");
|
map.put("c", "3");
|
||||||
map.put("d", "4");
|
map.put("d", "4");
|
||||||
|
|
||||||
Map<String, String> map2 = MapUtil.filter(map, t -> Convert.toInt(t.getValue()) % 2 == 0);
|
final Map<String, String> map2 = MapUtil.filter(map, t -> Convert.toInt(t.getValue()) % 2 == 0);
|
||||||
|
|
||||||
Assert.assertEquals(2, map2.size());
|
Assert.assertEquals(2, map2.size());
|
||||||
|
|
||||||
@ -64,37 +65,37 @@ public class MapUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void mapTest() {
|
public void mapTest() {
|
||||||
// Add test like a foreigner
|
// Add test like a foreigner
|
||||||
Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder()
|
final Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder()
|
||||||
.put(0, "lovely")
|
.put(0, "lovely")
|
||||||
.put(1, "friendly")
|
.put(1, "friendly")
|
||||||
.put(2, "happily")
|
.put(2, "happily")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Map<Integer, String> resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());
|
final Map<Integer, String> resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());
|
||||||
|
|
||||||
Assert.assertEquals("lovely girl", resultMap.get(0));
|
Assert.assertEquals("lovely girl", resultMap.get(0));
|
||||||
Assert.assertEquals("friendly boy", resultMap.get(1));
|
Assert.assertEquals("friendly boy", resultMap.get(1));
|
||||||
Assert.assertEquals("happily child", resultMap.get(2));
|
Assert.assertEquals("happily child", resultMap.get(2));
|
||||||
|
|
||||||
// 下单用户,Queue表示正在 .排队. 抢我抢不到的二次元周边!
|
// 下单用户,Queue表示正在 .排队. 抢我抢不到的二次元周边!
|
||||||
Queue<String> customers = new ArrayDeque<>(Arrays.asList("刑部尚书手工耿", "木瓜大盗大漠叔", "竹鼠发烧找华农", "朴实无华朱一旦"));
|
final Queue<String> customers = new ArrayDeque<>(Arrays.asList("刑部尚书手工耿", "木瓜大盗大漠叔", "竹鼠发烧找华农", "朴实无华朱一旦"));
|
||||||
// 分组
|
// 分组
|
||||||
List<Group> groups = Stream.iterate(0L, i -> ++i).limit(4).map(i -> Group.builder().id(i).build()).collect(Collectors.toList());
|
final List<Group> groups = Stream.iterate(0L, i -> ++i).limit(4).map(i -> Group.builder().id(i).build()).collect(Collectors.toList());
|
||||||
// 如你所见,它是一个map,key由用户id,value由用户组成
|
// 如你所见,它是一个map,key由用户id,value由用户组成
|
||||||
Map<Long, User> idUserMap = Stream.iterate(0L, i -> ++i).limit(4).map(i -> User.builder().id(i).name(customers.poll()).build()).collect(Collectors.toMap(User::getId, Function.identity()));
|
final Map<Long, User> idUserMap = Stream.iterate(0L, i -> ++i).limit(4).map(i -> User.builder().id(i).name(customers.poll()).build()).collect(Collectors.toMap(User::getId, Function.identity()));
|
||||||
// 如你所见,它是一个map,key由分组id,value由用户ids组成,典型的多对多关系
|
// 如你所见,它是一个map,key由分组id,value由用户ids组成,典型的多对多关系
|
||||||
Map<Long, List<Long>> groupIdUserIdsMap = groups.stream().flatMap(group -> idUserMap.keySet().stream().map(userId -> UserGroup.builder().groupId(group.getId()).userId(userId).build()))
|
final Map<Long, List<Long>> groupIdUserIdsMap = groups.stream().flatMap(group -> idUserMap.keySet().stream().map(userId -> UserGroup.builder().groupId(group.getId()).userId(userId).build()))
|
||||||
.collect(Collectors.groupingBy(UserGroup::getGroupId, Collectors.mapping(UserGroup::getUserId, Collectors.toList())));
|
.collect(Collectors.groupingBy(UserGroup::getGroupId, Collectors.mapping(UserGroup::getUserId, Collectors.toList())));
|
||||||
|
|
||||||
// 神奇的魔法发生了, 分组id和用户ids组成的map,竟然变成了订单编号和用户实体集合组成的map
|
// 神奇的魔法发生了, 分组id和用户ids组成的map,竟然变成了订单编号和用户实体集合组成的map
|
||||||
Map<Long, List<User>> groupIdUserMap = MapUtil.map(groupIdUserIdsMap, (groupId, userIds) -> userIds.stream().map(idUserMap::get).collect(Collectors.toList()));
|
final Map<Long, List<User>> groupIdUserMap = MapUtil.map(groupIdUserIdsMap, (groupId, userIds) -> userIds.stream().map(idUserMap::get).collect(Collectors.toList()));
|
||||||
|
|
||||||
// 然后你就可以拿着这个map,去封装groups,使其能够在订单数据带出客户信息啦
|
// 然后你就可以拿着这个map,去封装groups,使其能够在订单数据带出客户信息啦
|
||||||
groups.forEach(group -> Opt.ofNullable(group.getId()).map(groupIdUserMap::get).ifPresent(group::setUsers));
|
groups.forEach(group -> Opt.ofNullable(group.getId()).map(groupIdUserMap::get).ifPresent(group::setUsers));
|
||||||
|
|
||||||
// 下面是测试报告
|
// 下面是测试报告
|
||||||
groups.forEach(group -> {
|
groups.forEach(group -> {
|
||||||
List<User> users = group.getUsers();
|
final List<User> users = group.getUsers();
|
||||||
Assert.assertEquals("刑部尚书手工耿", users.get(0).getName());
|
Assert.assertEquals("刑部尚书手工耿", users.get(0).getName());
|
||||||
Assert.assertEquals("木瓜大盗大漠叔", users.get(1).getName());
|
Assert.assertEquals("木瓜大盗大漠叔", users.get(1).getName());
|
||||||
Assert.assertEquals("竹鼠发烧找华农", users.get(2).getName());
|
Assert.assertEquals("竹鼠发烧找华农", users.get(2).getName());
|
||||||
@ -106,7 +107,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterMapWrapperTest() {
|
public void filterMapWrapperTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap();
|
final Map<String, String> map = MapUtil.newHashMap();
|
||||||
map.put("a", "1");
|
map.put("a", "1");
|
||||||
map.put("b", "2");
|
map.put("b", "2");
|
||||||
map.put("c", "3");
|
map.put("c", "3");
|
||||||
@ -114,7 +115,7 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
final Map<String, String> camelCaseMap = MapUtil.toCamelCaseMap(map);
|
final Map<String, String> camelCaseMap = MapUtil.toCamelCaseMap(map);
|
||||||
|
|
||||||
Map<String, String> map2 = MapUtil.filter(camelCaseMap, t -> Convert.toInt(t.getValue()) % 2 == 0);
|
final Map<String, String> map2 = MapUtil.filter(camelCaseMap, t -> Convert.toInt(t.getValue()) % 2 == 0);
|
||||||
|
|
||||||
Assert.assertEquals(2, map2.size());
|
Assert.assertEquals(2, map2.size());
|
||||||
|
|
||||||
@ -124,13 +125,13 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterContainsTest() {
|
public void filterContainsTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap();
|
final Map<String, String> map = MapUtil.newHashMap();
|
||||||
map.put("abc", "1");
|
map.put("abc", "1");
|
||||||
map.put("bcd", "2");
|
map.put("bcd", "2");
|
||||||
map.put("def", "3");
|
map.put("def", "3");
|
||||||
map.put("fgh", "4");
|
map.put("fgh", "4");
|
||||||
|
|
||||||
Map<String, String> map2 = MapUtil.filter(map, t -> StrUtil.contains(t.getKey(), "bc"));
|
final Map<String, String> map2 = MapUtil.filter(map, t -> StrUtil.contains(t.getKey(), "bc"));
|
||||||
Assert.assertEquals(2, map2.size());
|
Assert.assertEquals(2, map2.size());
|
||||||
Assert.assertEquals("1", map2.get("abc"));
|
Assert.assertEquals("1", map2.get("abc"));
|
||||||
Assert.assertEquals("2", map2.get("bcd"));
|
Assert.assertEquals("2", map2.get("bcd"));
|
||||||
@ -138,13 +139,13 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void editTest() {
|
public void editTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap();
|
final Map<String, String> map = MapUtil.newHashMap();
|
||||||
map.put("a", "1");
|
map.put("a", "1");
|
||||||
map.put("b", "2");
|
map.put("b", "2");
|
||||||
map.put("c", "3");
|
map.put("c", "3");
|
||||||
map.put("d", "4");
|
map.put("d", "4");
|
||||||
|
|
||||||
Map<String, String> map2 = MapUtil.edit(map, t -> {
|
final Map<String, String> map2 = MapUtil.edit(map, t -> {
|
||||||
// 修改每个值使之*10
|
// 修改每个值使之*10
|
||||||
t.setValue(t.getValue() + "0");
|
t.setValue(t.getValue() + "0");
|
||||||
return t;
|
return t;
|
||||||
@ -160,13 +161,13 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void reverseTest() {
|
public void reverseTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap();
|
final Map<String, String> map = MapUtil.newHashMap();
|
||||||
map.put("a", "1");
|
map.put("a", "1");
|
||||||
map.put("b", "2");
|
map.put("b", "2");
|
||||||
map.put("c", "3");
|
map.put("c", "3");
|
||||||
map.put("d", "4");
|
map.put("d", "4");
|
||||||
|
|
||||||
Map<String, String> map2 = MapUtil.reverse(map);
|
final Map<String, String> map2 = MapUtil.reverse(map);
|
||||||
|
|
||||||
Assert.assertEquals("a", map2.get("1"));
|
Assert.assertEquals("a", map2.get("1"));
|
||||||
Assert.assertEquals("b", map2.get("2"));
|
Assert.assertEquals("b", map2.get("2"));
|
||||||
@ -176,13 +177,13 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toObjectArrayTest() {
|
public void toObjectArrayTest() {
|
||||||
Map<String, String> map = MapUtil.newHashMap(true);
|
final Map<String, String> map = MapUtil.newHashMap(true);
|
||||||
map.put("a", "1");
|
map.put("a", "1");
|
||||||
map.put("b", "2");
|
map.put("b", "2");
|
||||||
map.put("c", "3");
|
map.put("c", "3");
|
||||||
map.put("d", "4");
|
map.put("d", "4");
|
||||||
|
|
||||||
Object[][] objectArray = MapUtil.toObjectArray(map);
|
final Object[][] objectArray = MapUtil.toObjectArray(map);
|
||||||
Assert.assertEquals("a", objectArray[0][0]);
|
Assert.assertEquals("a", objectArray[0][0]);
|
||||||
Assert.assertEquals("1", objectArray[0][1]);
|
Assert.assertEquals("1", objectArray[0][1]);
|
||||||
Assert.assertEquals("b", objectArray[1][0]);
|
Assert.assertEquals("b", objectArray[1][0]);
|
||||||
@ -195,18 +196,18 @@ public class MapUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sortJoinTest(){
|
public void sortJoinTest(){
|
||||||
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
final Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
|
||||||
.put("key1", "value1")
|
.put("key1", "value1")
|
||||||
.put("key3", "value3")
|
.put("key3", "value3")
|
||||||
.put("key2", "value2").build();
|
.put("key2", "value2").build();
|
||||||
|
|
||||||
String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false);
|
final String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false);
|
||||||
Assert.assertEquals("key1value1key2value2key3value3", join1);
|
Assert.assertEquals("key1value1key2value2key3value3", join1);
|
||||||
|
|
||||||
String join2 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, "123");
|
final String join2 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, "123");
|
||||||
Assert.assertEquals("key1value1key2value2key3value3123", join2);
|
Assert.assertEquals("key1value1key2value2key3value3123", join2);
|
||||||
|
|
||||||
String join3 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, "123", "abc");
|
final String join3 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, "123", "abc");
|
||||||
Assert.assertEquals("key1value1key2value2key3value3123abc", join3);
|
Assert.assertEquals("key1value1key2value2key3value3123abc", join3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +237,22 @@ public class MapUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void renameKeyTest() {
|
public void renameKeyTest() {
|
||||||
final Dict v1 = Dict.of().set("id", 12).set("name", "张三").set("age", null);
|
final Dict v1 = Dict.of().set("id", 12).set("name", "张三").set("age", null);
|
||||||
Map<String, Object> map = MapUtil.renameKey(v1, "name", "newName");
|
final Map<String, Object> map = MapUtil.renameKey(v1, "name", "newName");
|
||||||
Assert.assertEquals("张三", map.get("newName"));
|
Assert.assertEquals("张三", map.get("newName"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue3162Test() {
|
||||||
|
final Map<String, Object> map = new HashMap<String, Object>() {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
{
|
||||||
|
put("a", "1");
|
||||||
|
put("b", "2");
|
||||||
|
put("c", "3");
|
||||||
|
}};
|
||||||
|
final Map<String, Object> filtered = MapUtil.filter(map, "a", "b");
|
||||||
|
Assert.assertEquals(2, filtered.size());
|
||||||
|
Assert.assertEquals("1", filtered.get("a"));
|
||||||
|
Assert.assertEquals("2", filtered.get("b"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user