修复MapUtil工具使用filter方法构造传入参数结果问题

This commit is contained in:
Looly 2023-06-25 16:40:12 +08:00
parent 4f17d18e05
commit e205d2659a
2 changed files with 33 additions and 18 deletions

View File

@ -253,16 +253,21 @@ public class MapUtil extends MapGetUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> createMap(final Class<?> mapType, final Supplier<Map<K, V>> defaultMap) { public static <K, V> Map<K, V> createMap(final Class<?> mapType, final Supplier<Map<K, V>> defaultMap) {
if (null == mapType || mapType.isAssignableFrom(AbstractMap.class)) { Map<K, V> result = null;
return defaultMap.get(); if (null != mapType && !mapType.isAssignableFrom(AbstractMap.class)) {
} else { result = (Map<K, V>) ConstructorUtil.newInstanceIfPossible(mapType);
try {
return (Map<K, V>) ConstructorUtil.newInstance(mapType);
} catch (final Exception e) {
// 不支持的map类型返回默认的HashMap
return defaultMap.get();
}
} }
if(null == result){
result = defaultMap.get();
}
if(!result.isEmpty()){
// issue#3162@Github在构造中put值会导致新建map带有值内容此处清空
result.clear();
}
return result;
} }
// ----------------------------------------------------------------------------------------------- value of // ----------------------------------------------------------------------------------------------- value of
@ -657,16 +662,12 @@ public class MapUtil extends MapGetUtil {
* @param editor 编辑器接口 * @param editor 编辑器接口
* @return 编辑后的Map * @return 编辑后的Map
*/ */
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> edit(final Map<K, V> map, final UnaryOperator<Entry<K, V>> editor) { public static <K, V> Map<K, V> edit(final Map<K, V> map, final UnaryOperator<Entry<K, V>> editor) {
if (null == map || null == editor) { if (null == map || null == editor) {
return map; return map;
} }
Map<K, V> map2 = ConstructorUtil.newInstanceIfPossible(map.getClass()); final Map<K, V> map2 = createMap(map.getClass(), ()-> new HashMap<>(map.size(), 1f));
if (null == map2) {
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map)) { if (isEmpty(map)) {
return map2; return map2;
} }
@ -681,6 +682,8 @@ public class MapUtil extends MapGetUtil {
return map2; return map2;
} }
/** /**
* 过滤<br> * 过滤<br>
* 过滤过程通过传入的Editor实现来返回需要的元素内容这个Filter实现可以实现以下功能 * 过滤过程通过传入的Editor实现来返回需要的元素内容这个Filter实现可以实现以下功能
@ -739,10 +742,7 @@ public class MapUtil extends MapGetUtil {
return map; return map;
} }
Map<K, V> map2 = ConstructorUtil.newInstanceIfPossible(map.getClass()); final Map<K, V> map2 = createMap(map.getClass(), ()-> new HashMap<>(map.size(), 1f));
if (null == map2) {
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map)) { if (isEmpty(map)) {
return map2; return map2;
} }

View File

@ -292,4 +292,19 @@ public class MapUtilTest {
final Map<String, Object> map = MapUtil.removeNullValue(v1); final Map<String, Object> map = MapUtil.removeNullValue(v1);
Assertions.assertEquals(1, map.size()); Assertions.assertEquals(1, map.size());
} }
@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");
Assertions.assertEquals(2, filtered.size());
Assertions.assertEquals("1", filtered.get("a"));
Assertions.assertEquals("2", filtered.get("b"));
}
} }