Java : Map 示例
Java 中的 Map (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 Map<K,V> 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
将键映射到值的对象。映射不能包含重复的键;每个键最多可以映射到一个值。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.get("a")); // 10
System.out.println(map.get("b")); // 20
System.out.println(map.get("X")); // null
map.remove("b");
System.out.println(map); // {a=10, c=30}
Methods
void clear ()
从此映射中删除所有映射(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
default V compute (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
尝试计算指定键及其当前映射值的映射,如果没有当前映射则为 null(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
if (value == null) {
return -100;
} else {
return value * 10;
}
}
};
final var ret1 = map.compute("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}
final var ret2 = map.compute("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}
final var ret3 = map.compute("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}
final var ret4 = map.compute("X", func);
System.out.println(ret4); // -100
System.out.println(map); // {a=10, b=20, c=30, X=-100}
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var ret = map.compute("b", (_, _) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}
default V computeIfAbsent (K key, Function<? super K,? extends V> mappingFunction)
如果指定的键尚未与值关联(或映射到 null),则尝试使用给定的映射函数计算其值并将其输入到此映射中,除非为 null(可选操作)。 (机器翻译)
final Map<String, String> map = new HashMap<>();
map.put("a", "aaa");
map.put("b", "bbb");
System.out.println(map); // {a=aaa, b=bbb}
final var func = new Function<String, String>() {
@Override
public String apply(String key) {
if ("c".equals(key)) {
return "ccc";
} else {
return null;
}
}
};
final var ret1 = map.computeIfAbsent("a", func);
System.out.println(ret1); // aaa
System.out.println(map); // {a=aaa, b=bbb}
final var ret2 = map.computeIfAbsent("b", func);
System.out.println(ret2); // bbb
System.out.println(map); // {a=aaa, b=bbb}
final var ret3 = map.computeIfAbsent("c", func);
System.out.println(ret3); // ccc
System.out.println(map); // {a=aaa, b=bbb, c=ccc}
final var ret4 = map.computeIfAbsent("d", func);
System.out.println(ret4); // null
System.out.println(map); // {a=aaa, b=bbb, c=ccc}
final Map<String, List<String>> map = new HashMap<>();
System.out.println(map); // {}
map.computeIfAbsent("a", (_) -> new ArrayList<>()).add("a-1");
System.out.println(map); // {a=[a-1]}
map.computeIfAbsent("a", (_) -> new ArrayList<>()).add("a-2");
System.out.println(map); // {a=[a-1, a-2]}
map.computeIfAbsent("b", (_) -> new ArrayList<>()).add("b-1");
System.out.println(map); // {a=[a-1, a-2], b=[b-1]}
default V computeIfPresent (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
如果指定键的值存在且非空,则尝试根据该键及其当前映射值计算新的映射(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
return value * 10;
}
};
final var ret1 = map.computeIfPresent("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}
final var ret2 = map.computeIfPresent("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}
final var ret3 = map.computeIfPresent("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}
final var ret4 = map.computeIfPresent("X", func);
System.out.println(ret4); // null
System.out.println(map); // {a=10, b=20, c=30}
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var ret = map.computeIfPresent("b", (_, _) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}
boolean containsKey (Object key)
如果此映射包含指定键的映射,则返回 true。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.containsKey("a")); // true
System.out.println(map.containsKey("b")); // true
System.out.println(map.containsKey("c")); // true
System.out.println(map.containsKey("X")); // false
System.out.println(map.containsKey("")); // false
boolean containsValue (Object value)
如果此映射将一个或多个键映射到指定值,则返回 true。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "X", 3);
System.out.println(map); // {a=1, b=2, c=3, X=3}
System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(1)); // true
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // true
System.out.println(map.containsValue(4)); // false
static <K, V> Map<K,V> copyOf (Map<? extends K,? extends V> map)
返回包含给定 Map 的条目的不可修改的 Map。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var copiedMap = Map.copyOf(map);
System.out.println(copiedMap); // {a=1, b=2, c=3}
System.out.println(map.equals(copiedMap)); // true
map.clear();
System.out.println(map); // {}
System.out.println(copiedMap); // {a=1, b=2, c=3}
//copiedMap.put("d", 4); // UnsupportedOperationException
//copiedMap.clear(); // UnsupportedOperationException
static <K, V> Map.Entry<K,V> entry (K k, V v)
返回包含给定键和值的不可修改的 Map.Entry。 (机器翻译)
final var entry1 = Map.entry("a", 1);
System.out.println(entry1); // a=1
//entry1.setValue(5); // UnsupportedOperationException
final var entry2 = Map.entry("b", 2);
System.out.println(entry2); // b=2
final var entry3 = Map.entry("c", 3);
System.out.println(entry3); // c=3
final var map = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map); // {a=1, b=2, c=3}
Set<Map.Entry<K,V>> entrySet ()
返回此映射中包含的映射的集合视图。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var entries = map.entrySet();
System.out.println(entries); // [a=1, b=2, c=3]
map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(entries); // [a=1, b=20, c=3]
entries.remove(Map.entry("a", 1));
System.out.println(map); // {b=20, c=3}
System.out.println(entries); // [b=20, c=3]
for (final var entry : entries) {
entry.setValue(entry.getValue() * 10);
}
System.out.println(map); // {b=200, c=30}
System.out.println(entries); // [b=200, c=30]
boolean equals (Object o)
将指定对象与此映射进行比较以确定其是否相等。 (机器翻译)
final var map1 = Map.of("a", 1, "b", 2, "c", 3);
final var map2 = Map.of("a", 1, "b", 2, "c", 3);
final var map3 = Map.of("A", 1, "B", 2, "C", 3);
final var map4 = Map.of("a", 10, "b", 20, "c", 30);
System.out.println(map1.equals(map2)); // true
System.out.println(map1.equals(map3)); // false
System.out.println(map1.equals(map4)); // false
default void forEach (BiConsumer<? super K,? super V> action)
对此映射中的每个条目执行给定的操作,直到所有条目都已处理或该操作引发异常。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3);
System.out.println(map); // {a=1, b=2, c=3}
map.forEach((key, value) -> {
System.out.printf("key = %s : value = %d%n", key, value);
});
// Result
// ↓
//key = a : value = 1
//key = b : value = 2
//key = c : value = 3
V get (Object key)
返回指定键映射到的值,如果此映射不包含该键的映射,则返回 null。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.get("a")); // 1
System.out.println(map.get("b")); // 2
System.out.println(map.get("c")); // 3
System.out.println(map.get("X")); // null
System.out.println(map.get("")); // null
default V getOrDefault (Object key, V defaultValue)
返回指定键映射到的值;如果此映射不包含该键的映射,则返回默认值。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.getOrDefault("a", 10)); // 1
System.out.println(map.getOrDefault("b", 20)); // 2
System.out.println(map.getOrDefault("c", 30)); // 3
System.out.println(map.getOrDefault("X", 40)); // 40
System.out.println(map.getOrDefault("", 50)); // 50
int hashCode ()
返回此映射的哈希码值。 (机器翻译)
final var map1 = Map.of();
System.out.println(map1); // {}
System.out.println(map1.hashCode()); // 0
final var map2 = Map.of("a", 1, "b", 2);
System.out.println(map2); // {a=1, b=2}
System.out.println(map2.hashCode()); // 192
final var map3 = Map.of("A", 1, "B", 2);
System.out.println(map3); // {A=1, B=2}
System.out.println(map3.hashCode()); // 128
final var map4 = Map.of("a", 10, "b", 20);
System.out.println(map4); // {a=10, b=20}
System.out.println(map4.hashCode()); // 225
boolean isEmpty ()
如果此映射不包含键值映射,则返回 true。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
System.out.println(map.isEmpty()); // false
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
System.out.println(map.isEmpty()); // true
Set<K> keySet ()
返回此映射中包含的键的 Set 视图。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var keys = map.keySet();
System.out.println(keys); // [a, b, c]
map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]
keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]
default V merge (K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
如果指定的键尚未与值关联或者与 null 关联,则将其与给定的非 null 值关联(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer oldValue, Integer newValue) {
return oldValue + newValue;
}
};
final var ret1 = map.merge("a", 1000, func);
System.out.println(ret1); // 1001
System.out.println(map); // {a=1001, b=2, c=3}
final var ret2 = map.merge("b", 1000, func);
System.out.println(ret2); // 1002
System.out.println(map); // {a=1001, b=1002, c=3}
final var ret3 = map.merge("c", 1000, func);
System.out.println(ret3); // 1003
System.out.println(map); // {a=1001, b=1002, c=1003}
final var ret4 = map.merge("d", 1000, func);
System.out.println(ret4); // 1000
System.out.println(map); // {a=1001, b=1002, c=1003, d=1000}
final var ret5 = map.merge("b", 1000, (_, _) -> null);
System.out.println(ret5); // null
System.out.println(map); // {a=1001, c=1003, d=1000}
static <K, V> Map<K,V> of ()
返回包含零映射的不可修改映射。 (机器翻译)
final var map = Map.of();
System.out.println(map); // {}
System.out.println(map.size()); // 0
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1)
返回包含单个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.size()); // 1
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2)
返回包含两个映射的不可修改的映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2);
System.out.println(map); // {b=2, a=1}
System.out.println(map.size()); // 2
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3)
返回包含三个映射的不可修改的映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3);
System.out.println(map); // {c=3, b=2, a=1}
System.out.println(map.size()); // 3
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
返回包含四个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4);
System.out.println(map); // {d=4, a=1, b=2, c=3}
System.out.println(map.size()); // 4
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
返回包含五个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5);
System.out.println(map); // {d=4, e=5, a=1, b=2, c=3}
System.out.println(map.size()); // 5
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
返回包含六个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5,
"f", 6);
System.out.println(map); // {f=6, e=5, d=4, c=3, b=2, a=1}
System.out.println(map.size()); // 6
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
返回包含七个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5,
"f", 6, "g", 7);
System.out.println(map); // {g=7, a=1, b=2, c=3, d=4, e=5, f=6}
System.out.println(map.size()); // 7
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
返回包含八个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5,
"f", 6, "g", 7, "h", 8);
System.out.println(map); // {a=1, h=8, g=7, f=6, e=5, d=4, c=3, b=2}
System.out.println(map.size()); // 8
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
返回包含九个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5,
"f", 6, "g", 7, "h", 8, "i", 9);
System.out.println(map); // {c=3, d=4, e=5, f=6, g=7, h=8, i=9, a=1, b=2}
System.out.println(map.size()); // 9
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
返回包含十个映射的不可修改映射。 (机器翻译)
final var map = Map.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5,
"f", 6, "g", 7, "h", 8, "i", 9, "j", 10);
System.out.println(map); // {d=4, e=5, f=6, g=7, h=8, i=9, j=10, a=1, b=2, c=3}
System.out.println(map.size()); // 10
//map.put("z", 999); // UnsupportedOperationException
static <K, V> Map<K,V> ofEntries (Map.Entry<? extends K,? extends V>... entries)
返回一个不可修改的映射,其中包含从给定条目中提取的键和值。 (机器翻译)
final var entry1 = Map.entry("a", 1);
System.out.println(entry1); // a=1
//entry1.setValue(5); // UnsupportedOperationException
final var entry2 = Map.entry("b", 2);
System.out.println(entry2); // b=2
final var entry3 = Map.entry("c", 3);
System.out.println(entry3); // c=3
final var map = Map.ofEntries(entry1, entry2, entry3);
System.out.println(map); // {a=1, b=2, c=3}
V put (K key, V value)
将指定的值与此映射中的指定键关联(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.put("a", 1)); // null
System.out.println(map); // {a=1}
System.out.println(map.put("b", 2)); // null
System.out.println(map); // {a=1, b=2}
System.out.println(map.put("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.put("a", 999)); // 1
System.out.println(map); // {a=999, b=2, c=3}
void putAll (Map<? extends K,? extends V> m)
将指定映射中的所有映射复制到此映射(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
map.putAll(Map.of());
System.out.println(map); // {}
map.putAll(Map.of("a", 1, "b", 2));
System.out.println(map); // {a=1, b=2}
map.putAll(Map.of("c", 3, "d", 4, "e", 5));
System.out.println(map); // {a=1, b=2, c=3, d=4, e=5}
map.putAll(Map.of("a", 901, "b", 902, "z", 100));
System.out.println(map); // {a=901, b=902, z=100, c=3, d=4, e=5}
default V putIfAbsent (K key, V value)
如果指定的键尚未与值关联(或映射到 null),则将其与给定的值关联并返回 null,否则返回当前值(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.putIfAbsent("a", 1)); // null
System.out.println(map); // {a=1}
System.out.println(map.putIfAbsent("b", 2)); // null
System.out.println(map); // {a=1, b=2}
System.out.println(map.putIfAbsent("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.putIfAbsent("a", 999)); // 1
System.out.println(map); // {a=1, b=2, c=3}
V remove (Object key)
如果存在键,则从此映射中移除该键的映射(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.remove("b")); // 2
System.out.println(map); // {a=1, c=3}
System.out.println(map.remove("z")); // null
System.out.println(map); // {a=1, c=3}
default boolean remove (Object key, Object value)
仅当指定键当前映射到指定值时才删除该条目(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.remove("a", 1)); // true
System.out.println(map); // {b=2, c=3}
System.out.println(map.remove("b", 1)); // false
System.out.println(map); // {b=2, c=3}
System.out.println(map.remove("b", 2)); // true
System.out.println(map); // {c=3}
System.out.println(map.remove("C", 3)); // false
System.out.println(map); // {c=3}
System.out.println(map.remove("d", 4)); // false
System.out.println(map); // {c=3}
default V replace (K key, V value)
仅当指定键当前映射到某个值时才替换该条目(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.replace("a", 10)); // 1
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 20)); // 2
System.out.println(map); // {a=10, b=20, c=3}
System.out.println(map.replace("z", 99)); // null
System.out.println(map); // {a=10, b=20, c=3}
default boolean replace (K key, V oldValue, V newValue)
仅当当前映射到指定值时才替换指定键的条目(可选操作)。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.replace("a", 1, 10)); // true
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 999, 20)); // false
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 2, 20)); // true
System.out.println(map); // {a=10, b=20, c=3}
System.out.println(map.replace("z", 3, 999)); // false
System.out.println(map); // {a=10, b=20, c=3}
default void replaceAll (BiFunction<? super K,? super V,? extends V> function)
用对该条目调用给定函数的结果替换每个条目的值,直到所有条目都已处理或该函数引发异常(可选操作)。 (机器翻译)
final var src = Map.of("a", 1, "b", 2, "c", 3);
{
final Map<String, Integer> map = new HashMap<>(src);
System.out.println(map); // {a=1, b=2, c=3}
map.replaceAll((_, v) -> v * 10);
System.out.println(map); // {a=10, b=20, c=30}
}
{
final Map<String, Integer> map = new HashMap<>(src);
System.out.println(map); // {a=1, b=2, c=3}
map.replaceAll((k, v) -> {
if (k.equals("a")) {
return v * 1000;
} else {
return v * 10;
}
});
System.out.println(map); // {a=1000, b=20, c=30}
}
{
final Map<String, Integer> map = new HashMap<>(src);
System.out.println(map); // {a=1, b=2, c=3}
map.replaceAll((_, v) -> {
if (v == 2) {
return v * 1000;
} else {
return v * 10;
}
});
System.out.println(map); // {a=10, b=2000, c=30}
}
{
final Map<String, Integer> map = new HashMap<>(src);
System.out.println(map); // {a=1, b=2, c=3}
map.replaceAll((_, _) -> null);
System.out.println(map); // {a=null, b=null, c=null}
}
int size ()
返回此映射中的键值映射的数量。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
System.out.println(map); // {}
System.out.println(map.size()); // 0
map.put("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.size()); // 1
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.size()); // 2
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
Collection<V> values ()
返回此映射中包含的值的 Collection 视图。 (机器翻译)
final Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var values = map.values();
System.out.println(values); // [1, 2, 3]
map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]
values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]
//values.add(4); // UnsupportedOperationException
相关文章
- API 示例