You can modify a HashMap in Java with put
methods to add key-value pairs, replace
methods to update values and remove
methods to remove key-value pair entries
Add key-value pairs into a HashMap
V put(K key, V value)
method adds the key-value mapping to a map. Ifkey
is existed, old value is replaced by the specified value
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.put("k2", 2);
System.out.println(map); // {k1=1, k2=2}
- Java 8+ method
V putIfAbsent(K key, V value)
adds the key-value mapping to a map either there's no associated or the key is associated withnull
. If there's an associated, return the current value
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.putIfAbsent("k3", 3);
map.putIfAbsent("k1", 4);
map.putIfAbsent("k2", 2);
System.out.println(map); // {k1=1, k2=2, k3=3}
void putAll(Map<? extends K, ? extends V> m)
copies all mappings from the specified map and adds to the caller map
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
Map<String, Integer> map2 = new HashMap<>();
map2.put("k3", 3);
map2.putAll(map);
System.out.println(map2); // {k1=1, k2=null, k3=3}
Update values in HashMap
- Java 8+
V replace(K key, V value)
method updates / replaces the old value to the specified value if the specified key is existed in map
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.replace("k2", 2);
map.replace("k3", 3); // {k1=1, k2=2}
- Java 8+
boolean replace(K key, V oldValue, V newValue)
method updates / replaces the specifiedoldValue
to the specifiednewValue
if the specifiedkey
is currently associated witholdValue
in map
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.replace("k1", 1, 10);
map.replace("k2", 2, 20); // {k1=10, k2=null}
- Java 8+
void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
method updates / replaces all current values to the value returned by thefunction
argument
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.replaceAll((k, v) -> 100)
System.out.println(map); // {k1=100, k2=100}
Remove HashMap entries
V remove(Object key)
method removes the entry by the specifiedkey
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.remove("k2");
map.remove("k3");
System.out.println(map); // {k1=1}
- Java 8+
boolean remove(Object key, Object value)
method removes the entry by the specifiedkey
andvalue
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.remove("k1", 10);
map.remove("k2", null);
System.out.println(map); // {k1=1}
void clear()
method removes all key-value mapping entries from map
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1);
map.put("k2", null);
System.out.println(map); // {k1=1, k2=null}
map.clear();
System.out.println(map); // {}