@Beta public class MapModifier<K,V> extends Object
封装一系列对 Map 数据的修改操作,修改 Map 的数据。可以用于 Map 的数据初始化等操作。
// MapModifier
MapModifier<String, Object> modifier = new MapModifier<String, Object>()
.putAll(commonProperties)
.put("username", "Ben")
.put("accountStatus", LOCKED);
// 从 Supplier 中获取 Map,并修改数据
Map<String, Object> map = modifier.getAndModify(HashMap::new);
// 可以灵活使用不同 Map 类型的不同构造器
Map<String, Object> map = modifier.getAndModify(() -> new HashMap<>(8));
Map<String, Object> map = modifier.getAndModify(() -> new HashMap<>(anotherMap));
Map<String, Object> map = modifier.getAndModify(TreeMap::new);
Map<String, Object> map = modifier.getAndModify(ConcurrentHashMap::new);
// 修改已有的 Map
modifier.modify(map);
// 创建一个有初始化数据的不可变的 Map
Map<String, Object> map = modifier.getUnmodifiableMap();
// 链式调用创建并初始化数据
Map<String, Object> map = new MapModifier<String, Object>()
.putAll(commonProperties)
.put("username", "Ben")
.put("accountStatus", LOCKED)
.getAndModify(HashMap::new);
| Constructor and Description |
|---|
MapModifier()
创建一个空的 MapModifier
|
MapModifier(int initialCapacity) |
| Modifier and Type | Method and Description |
|---|---|
MapModifier<K,V> |
clear()
清空
map |
MapModifier<K,V> |
computeIfAbsent(K key,
Function<? super K,? extends V> mappingFunction)
当
key 不存在时,计算对应的值,并添加到 map 中。 |
MapModifier<K,V> |
computeIfPresent(K key,
BiFunction<? super K,? super V,? extends V> remappingFunction)
当
key 存在时,计算对应的值,并添加到 map 中。 |
<T extends Map<K,V>> |
getAndModify(Supplier<T> mapSupplier)
修改
map |
Map<K,V> |
getConcurrentHashMap()
获取
ConcurrentHashMap |
Map<K,V> |
getHashMap()
获取
HashMap |
Map<K,V> |
getLinkedHashMap()
获取
LinkedHashMap |
int |
getOperatorCount()
获取操作数量
|
Map<K,V> |
getTreeMap()
获取
TreeMap |
Map<K,V> |
getUnmodifiableMap()
创建一个有初始化数据的不可变的
Map |
boolean |
hasOperations()
是否有操作
|
<T extends Map<K,V>> |
modify(T map)
修改
map |
MapModifier<K,V> |
put(K key,
V value)
添加一个键值对。
|
MapModifier<K,V> |
putAll(Map<? extends K,? extends V> otherMap)
添加多个键值对。
|
MapModifier<K,V> |
putIfAbsent(K key,
V value)
添加一个键值对,如果 key 已经存在,则不添加。
|
MapModifier<K,V> |
remove(K key)
删除
key。 |
String |
toString() |
public MapModifier()
public MapModifier(int initialCapacity)
public MapModifier<K,V> put(@Nullable K key, @Nullable V value)
注意:键值对是否允许为 null,由最终作用的 Map 类型决定。
key - 要添加的 keyvalue - 要添加的 valuepublic MapModifier<K,V> putIfAbsent(@Nullable K key, @Nullable V value)
注意:键值对是否允许为 null,由最终作用的 Map 类型决定。
key - 要添加的 keyvalue - 要添加的 valuepublic MapModifier<K,V> putAll(@Nullable Map<? extends K,? extends V> otherMap)
注意:键值对是否允许为 null,由最终作用的 Map 类型决定。
otherMap - 要添加的键值对集合。
如果为 null,则什么都不做。public MapModifier<K,V> computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
key 不存在时,计算对应的值,并添加到 map 中。
调用 Map.computeIfAbsent(Object, Function)。
注意:键值对是否允许为 null,由最终作用的 Map 类型决定。
key - 要添加的 keymappingFunction - 计算 key 对应的值public MapModifier<K,V> computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
key 存在时,计算对应的值,并添加到 map 中。
调用 Map.computeIfPresent(Object, BiFunction)。
注意:键值对是否允许为 null,由最终作用的 Map 类型决定。
key - 要添加的 keyremappingFunction - 计算 key 对应的值public MapModifier<K,V> remove(K key)
key。
注意:key 是否允许为 null,由最终作用的 Map 类型决定。
key - 要删除的 keypublic MapModifier<K,V> clear()
map@CheckForNull public <T extends Map<K,V>> T getAndModify(Supplier<T> mapSupplier)
mapmapSupplier - map 的 Suppliermap。
当从 mapSupplier 获取的 map 为 null 时,返回 null。public Map<K,V> getConcurrentHashMap()
ConcurrentHashMapConcurrentHashMappublic int getOperatorCount()
public boolean hasOperations()
true,否则返回 falseCopyright © 2026. All rights reserved.