This commit is contained in:
Looly 2021-06-16 02:36:39 +08:00
parent 26064f146c
commit ad40ca5670

View File

@ -680,12 +680,17 @@ public class MapUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> filter(Map<K, V> map, K... keys) { public static <K, V> Map<K, V> filter(Map<K, V> map, K... keys) {
final Map<K, V> map2 = ObjectUtil.clone(map); Map<K, V> map2 = ObjectUtil.clone(map);
if (isEmpty(map2)) { if (isEmpty(map2)) {
return map2; return map2;
} }
try {
map2.clear(); map2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
map2 = new HashMap<>();
}
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));