This commit is contained in:
Looly 2022-03-03 21:52:30 +08:00
parent 4c1a729e97
commit d1dea5c88e
5 changed files with 39 additions and 33 deletions

View File

@ -1178,20 +1178,10 @@ public class CollUtil {
return collection;
}
Collection<T> collection2 = ObjectUtil.clone(collection);
if (null == collection2) {
// 不支持clone
collection2 = create(collection.getClass());
}
if (isEmpty(collection2)) {
final Collection<T> collection2 = create(collection.getClass());
if (isEmpty(collection)) {
return collection2;
}
try {
collection2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
collection2 = new ArrayList<>();
}
T modified;
for (T t : collection) {

View File

@ -7,7 +7,6 @@ import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
@ -623,25 +622,19 @@ public class MapUtil {
* @param editor 编辑器接口
* @return 编辑后的Map
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> edit(Map<K, V> map, Editor<Entry<K, V>> editor) {
if (null == map || null == editor) {
return map;
}
Map<K, V> map2 = ObjectUtil.clone(map);
Map<K, V> map2 = ReflectUtil.newInstanceIfPossible(map.getClass());
if(null == map2){
// 不支持clone
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map2)) {
if (isEmpty(map)) {
return map2;
}
try {
map2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
map2 = new HashMap<>(map.size(), 1f);
}
Entry<K, V> modified;
for (Entry<K, V> entry : map.entrySet()) {
@ -690,20 +683,14 @@ public class MapUtil {
if(null == map || null == keys){
return map;
}
Map<K, V> map2 = ObjectUtil.clone(map);
Map<K, V> map2 = ReflectUtil.newInstanceIfPossible(map.getClass());
if(null == map2){
// 不支持clone
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map2)) {
if (isEmpty(map)) {
return map2;
}
try {
map2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
map2 = new HashMap<>();
}
for (K key : keys) {
if (map.containsKey(key)) {

View File

@ -1,5 +1,7 @@
package cn.hutool.core.map;
import cn.hutool.core.util.ObjectUtil;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
@ -30,7 +32,7 @@ public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, S
*/
protected static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
private final Map<K, V> raw;
private Map<K, V> raw;
/**
* 构造
@ -199,5 +201,14 @@ public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, S
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
return raw.merge(key, value, remappingFunction);
}
@Override
public MapWrapper<K, V> clone() throws CloneNotSupportedException {
@SuppressWarnings("unchecked")
final MapWrapper<K, V> clone = (MapWrapper<K, V>) super.clone();
clone.raw = ObjectUtil.clone(raw);
return clone;
}
//---------------------------------------------------------------------------- Override default methods end
}

View File

@ -848,7 +848,7 @@ public class ReflectUtil {
*
* @param <T> 对象类型
* @param beanClass 被构造的类
* @return 构造后的对象
* @return 构造后的对象构造失败返回{@code null}
*/
@SuppressWarnings("unchecked")
public static <T> T newInstanceIfPossible(Class<T> beanClass) {

View File

@ -26,6 +26,24 @@ public class MapUtilTest {
Assert.assertEquals("4", map2.get("d"));
}
@Test
public void filterMapWrapperTest() {
Map<String, String> map = MapUtil.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
final Map<String, String> camelCaseMap = MapUtil.toCamelCaseMap(map);
Map<String, String> map2 = MapUtil.filter(camelCaseMap, t -> Convert.toInt(t.getValue()) % 2 == 0);
Assert.assertEquals(2, map2.size());
Assert.assertEquals("2", map2.get("b"));
Assert.assertEquals("4", map2.get("d"));
}
@Test
public void filterContainsTest() {
Map<String, String> map = MapUtil.newHashMap();