Map常用方法

以下都是以HashMap做例子

添加、删除、修改操作

put&putAll(添加、修改)

Modifier and Type Method Description
V put(K key, V value) Associates the specified value with the specified key in this map (optional operation).

put用法举例:

HashMap hashMap = new HashMap();

// 添加
hashMap.put("name", "gwx");
hashMap.put("age", 36);
System.out.println(hashMap); // {name=gwx, age=36}

// 修改
hashMap.put("name", "snn");
hashMap.put("age", 45);
System.out.println(hashMap); // {name=snn, age=45}

putAll用法举例:

HashMap hashMap1 = new HashMap();
HashMap hashMap2 = new HashMap();
HashMap hashMap3 = new HashMap();

hashMap1.put("name", "gwx");
hashMap1.put("age", 36);
System.out.println(hashMap1); // {name=gwx, age=36}

// 添加
hashMap2.put("hobby", "basketball,movies");
hashMap1.putAll(hashMap2);
System.out.println(hashMap1); // {name=gwx, age=36, hobby=basketball,movies}

// 修改
hashMap3.put("name", "james");
hashMap1.putAll(hashMap3);
System.out.println(hashMap1); // {name=james, age=36, hobby=basketball,movies}

replace&replaceAll

default V replace(K key, V value) Replaces the entry for the specified key only if it is currently mapped to some value.
default boolean replace(K key, V oldValue, V newValue) Replaces the entry for the specified key only if currently mapped to the specified value.
default void replaceAll(BiFunction<? super K,? super V,? extends V> function) Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

replace用法举例如下:

HashMap hashMap = new HashMap();
hashMap.put("name", "gwx");
hashMap.put("age", 36);

hashMap.replace("hobby", "milk");  // 修改失败,但不会报错
System.out.println(hashMap);// {name=gwx, age=36}

hashMap.replace("name", "james");  // key存在,成功修改
hashMap.replace("age", 36, 35);  // key/val都对应才能修改

replaceAll用法举例如下:

HashMap hashMap = new HashMap();
hashMap.put("username", "gwx");
hashMap.put("pass", "134521");

hashMap.replaceAll((key, value)->key + "|" + value);
System.out.println(hashMap);  // {pass=pass|134521, username=username|gwx}

remove&clear

V remove(Object key) Removes the mapping for a key from this map if it is present (optional operation).
default boolean remove(Object key, Object value) Removes the entry for the specified key only if it is currently mapped to the specified value.
void clear() Removes all of the mappings from this map (optional operation).
HashMap hashMap = new HashMap();
hashMap.put("name", "gwx");
hashMap.put("age", 36);

hashMap.remove("name");  // key满足即删除
hashMap.remove("age", 36);  // key/v同时满足才能删除

hashMap.put("name", "gwx");
hashMap.put("age", 36);

hashMap.clear();  // 清空所有

查询操作

isEmpty&size&get

boolean isEmpty() Returns true if this map contains no key-value mappings.
int size() Returns the number of key-value mappings in this map.
V get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

containsKey&containsValue

boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value) Returns true if this map maps one or more keys to the specified value.

遍历操作

Set<K> keySet() Returns a Set view of the keys contained in this map.
Collection<V> values() Returns a Collection view of the values contained in this map.
Set<Map.Entry<K,V>> entrySet() Returns a Set view of the mappings contained in this map.
HashMap hashMap = new HashMap();
hashMap.put("name", "james");
hashMap.put("age", 36);
hashMap.put("salary", 4000.0);
hashMap.put("team", "lakers");

// 遍历key
Set set = hashMap.keySet();
for (Object s:set) {
    System.out.println(s);
}

// 遍历val
Collection collection = hashMap.values();
for (Object c:collection) {
    System.out.println(c);
}

// 遍历key、val
for (Object s:set) {
    Object v = hashMap.get(s);
    System.out.println(s+"-"+v);
}

// 另一种方法遍历k、v(推荐)
Set set1 = hashMap.entrySet();

for (Object obj : set1) {
    Map.Entry entry = (Map.Entry) obj;
    System.out.println(entry.getKey() + ":" + entry.getValue());
}