Examples for usage of all methods of 'java.util.HashMap' with console output of example code.
Please click on method from below list to go to code example for usage of that method. Click [↓ Imports] to get import statements used in examples. To read javadoc of methods, click on [⿺ Javadoc] for that method.
Map<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
map.clear();
System.out.println("Map = " + map);
Output: Map = {one=uno, two=dos, three=tres} Map = {}
Tag: Example for clear method of class java.util.HashMap., HashMap clear function example , How to use clear method of HashMap?, Usage of HashMap.clear, HashMap.clear() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
Map<String, String> cloneMap = (HashMap<String, String>) map.clone();
System.out.println("cloneMap values = " + cloneMap);
System.out.println("Equality of elements -> " + (map.get("one") == cloneMap.get("one")) + " "
+ (map.get("one").equals(cloneMap.get("one"))));
Output: Map = {one=uno, two=dos, three=tres} cloneMap values = {two=dos, three=tres, one=uno} Equality of elements -> true true
Tag: Example for clone method of class java.util.HashMap., HashMap clone function example , How to use clone method of HashMap?, Usage of HashMap.clone, HashMap.clone() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
// Append string to one of the value for given key using map compute.
map.compute("two", (k, v) -> v.concat(" means 2"));
System.out.println("Map after remappingFunction to concat= " + map);
// Replace one of the value using compute
map.compute("two", (k, v) -> "II");
System.out.println("Map after remappingFunction with greek 2 = " + map);
// Compute for missing key value pair
map.compute("four", (k, v) -> "cuatro");
System.out.println("Map after remappingFunction adding new value = " + map);
// compute remappingFunction returning null. Value of matching key is removed.
map.compute("four", (k, v) -> null);
System.out.println("Map atfer remappingFunction returning null = " + map);
// compute remappingFunction throwing exception
map.compute("four", (k, v) -> {
throw new IllegalStateException();
});
Output: Map = {one=uno, two=dos, three=tres} Map after remappingFunction to concat= {one=uno, two=dos means 2, three=tres} Map after remappingFunction with greek 2 = {one=uno, two=II, three=tres} Map after remappingFunction adding new value = {four=cuatro, one=uno, two=II, three=tres} Map atfer remappingFunction returning null = {one=uno, two=II, three=tres} java.lang.IllegalStateException at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.lambda$4(HashMapExamples.java:72) at java.base/java.util.HashMap.compute(HashMap.java:1228) at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.test_compute(HashMapExamples.java:71)
Tag: Example for compute method of class java.util.HashMap., HashMap compute function example with arguments K key, java.util.function.BiFunction<? super K, ? super V, ? extends V> remappingFunction, How to use compute method of HashMap?, Usage of HashMap.compute, HashMap.compute() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
// Won't compute because key "two" is present.
map.computeIfAbsent("two", v -> v.concat(" means 2"));
System.out.println("Map after remappingFunction to concat= " + map);
// computeIfAbsent for missing key value pair
map.computeIfAbsent("four", v -> "cuatro");
System.out.println("Map after remappingFunction adding new value = " + map);
// remappingFunction returning null. No effect since value is not
// present.computeIfAbsent Won't compute for present value.
map.computeIfAbsent("five", v -> null);
System.out.println("Map atfer remappingFunction returning null [no effect] = " + map);
// computeIfAbsent remappingFunction throwing exception
map.computeIfAbsent("six", v -> {
throw new IllegalStateException();
});
Output: Map = {one=uno, two=dos, three=tres} Map after remappingFunction to concat= {one=uno, two=dos, three=tres} Map after remappingFunction adding new value = {four=cuatro, one=uno, two=dos, three=tres} Map atfer remappingFunction returning null [no effect] = {four=cuatro, one=uno, two=dos, three=tres} java.lang.IllegalStateException at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.lambda$8(HashMapExamples.java:101) at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1133) at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.test_computeIfAbsent(HashMapExamples.java:100)
Tag: Example for computeIfAbsent method of class java.util.HashMap., HashMap computeIfAbsent function example with arguments K key, java.util.function.Function<? super K, ? extends V> remappingFunction, How to use computeIfAbsent method of HashMap?, Usage of HashMap.computeIfAbsent, HashMap.computeIfAbsent() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
// Append string to one of the value for given key using map compute.
map.computeIfPresent("two", (k, v) -> v.concat(" means 2"));
System.out.println("Map after remappingFunction to concat= " + map);
// Replace one of the value using compute
map.computeIfPresent("two", (k, v) -> "II");
System.out.println("Map after remappingFunction with greek 2 = " + map);
// computeIfPresent for missing key value pair. No effect since computeIfPresent
// only computes for present value.
map.computeIfPresent("four", (k, v) -> "cuatro");
System.out.println("Map after remappingFunction for new value [no effect] = " + map);
// compute remappingFunction returning null. Value of matching key is removed.
map.computeIfPresent("three", (k, v) -> null);
System.out.println("Map atfer remappingFunction returning null = " + map);
// compute remappingFunction throwing exception
map.computeIfPresent("two", (k, v) -> {
throw new IllegalStateException();
});
Output: Map = {one=uno, two=dos, three=tres} Map after remappingFunction to concat= {one=uno, two=dos means 2, three=tres} Map after remappingFunction with greek 2 = {one=uno, two=II, three=tres} Map after remappingFunction for new value [no effect] = {one=uno, two=II, three=tres} Map atfer remappingFunction returning null = {one=uno, two=II} java.lang.IllegalStateException at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.lambda$13(HashMapExamples.java:134) at java.base/java.util.HashMap.computeIfPresent(HashMap.java:1175) at com.itsallbinary.javadocexamples.examples.java_util.HashMapExamples.test_computeIfPresent(HashMapExamples.java:133)
Tag: Example for computeIfPresent method of class java.util.HashMap., HashMap computeIfPresent function example with arguments K key, java.util.function.BiFunction<? super K, ? super V, ? extends V> remappingFunction, How to use computeIfPresent method of HashMap?, Usage of HashMap.computeIfPresent, HashMap.computeIfPresent() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println("Contains one " + map.containsKey("one"));
System.out.println("Contains four " + map.containsKey("four"));
Output: Map = {one=uno, two=dos, three=tres} Contains one true Contains four false
Tag: Example for containsKey method of class java.util.HashMap., HashMap containsKey function example with arguments java.lang.Object arg0, How to use containsKey method of HashMap?, Usage of HashMap.containsKey, HashMap.containsKey() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println("Contains uno " + map.containsKey("uno"));
System.out.println("Contains cinco " + map.containsKey("cinco"));
Output: Map = {one=uno, two=dos, three=tres} Contains uno false Contains cinco false
Tag: Example for containsValue method of class java.util.HashMap., HashMap containsValue function example with arguments java.lang.Object arg0, How to use containsValue method of HashMap?, Usage of HashMap.containsValue, HashMap.containsValue() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
// For each on map entry set
map.entrySet().forEach(e -> System.out.println("Key: " + e.getKey() + " Value: " + e.getValue()));
// Remove entry using entry set removeIf
map.entrySet().removeIf(e -> e.getKey().equals("two"));
System.out.println("Map after remove two = " + map);
// Remove entry using entry set remove Map.Entry
map.entrySet().remove(new Map.Entry() {
@Override
public Object getKey() {
return "one";
}
@Override
public Object getValue() {
return "uno";
}
@Override
public Object setValue(Object value) {
return null;
}
});
System.out.println("Map after remove one = " + map);
Output: Map = {one=uno, two=dos, three=tres} Key: one Value: uno Key: two Value: dos Key: three Value: tres Map after remove two = {one=uno, three=tres} Map after remove one = {three=tres}
Tag: Example for entrySet method of class java.util.HashMap., HashMap entrySet function example , How to use entrySet method of HashMap?, Usage of HashMap.entrySet, HashMap.entrySet() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
HashMap<String, String> anotherMap = new HashMap<>();
anotherMap.put("one", "uno");
anotherMap.put("two", "dos");
anotherMap.put("three", "tres");
System.out.println("anotherMap = " + map);
System.out.println("map.equals(anotherMap) = " + map.equals(anotherMap));
System.out.println("anotherMap.equals(map) = " + anotherMap.equals(map));
// Modify one value in anotherMap
anotherMap.replace("two", "II");
System.out.println("anotherMap = " + anotherMap);
System.out.println("map.equals(anotherMap) = " + map.equals(anotherMap));
// HashMap equals TreeMap. Equals works across different map implementation.
Map<String, String> treeMap = new TreeMap<>();
treeMap.put("one", "uno");
treeMap.put("two", "dos");
treeMap.put("three", "tres");
System.out.println("treeMap = " + treeMap);
System.out.println("map.equals(treeMap) = " + map.equals(treeMap));
Output: Map = {one=uno, two=dos, three=tres} anotherMap = {one=uno, two=dos, three=tres} map.equals(anotherMap) = true anotherMap.equals(map) = true anotherMap = {one=uno, two=II, three=tres} map.equals(anotherMap) = false treeMap = {one=uno, three=tres, two=dos} map.equals(treeMap) = true
Tag: Example for equals method of class java.util.AbstractMap., AbstractMap equals function example with arguments java.lang.Object arg0, How to use equals method of AbstractMap?, Usage of AbstractMap.equals, AbstractMap.equals() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
map.forEach((k, v) -> System.out.println("Key: " + k + " Value: " + v));
Output: Map = {one=uno, two=dos, three=tres} Key: one Value: uno Key: two Value: dos Key: three Value: tres
Tag: Example for forEach method of class java.util.HashMap., HashMap forEach function example with arguments java.util.function.BiConsumer<? super K, ? super V> arg0, How to use forEach method of HashMap?, Usage of HashMap.forEach, HashMap.forEach() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println(map.get("one") + " " + map.get("two") + " " + map.get("three"));
// Get using non existing key from map.
System.out.println(map.get("nine"));
// Map get using null key
System.out.println(map.get(null));
// Put null key in map & then get
map.put(null, "[NULL]");
System.out.println(map.get(null));
Output: Map = {one=uno, two=dos, three=tres} uno dos tres null null [NULL]
Tag: Example for get method of class java.util.HashMap., HashMap get function example with arguments java.lang.Object arg0, How to use get method of HashMap?, Usage of HashMap.get, HashMap.get() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println(map.getOrDefault("one", "NOT_FOUND"));
// getOrDefault using non existing key from map.
System.out.println(map.getOrDefault("nine", "NOT_FOUND"));
// Map getOrDefault using null key
System.out.println(map.getOrDefault(null, "NOT_FOUND"));
// Put null key in map & then getOrDefault
map.put(null, "[NULL]");
System.out.println(map.getOrDefault(null, "NOT_FOUND"));
Output: Map = {one=uno, two=dos, three=tres} uno NOT_FOUND NOT_FOUND [NULL]
Tag: Example for getOrDefault method of class java.util.HashMap., HashMap getOrDefault function example with arguments java.lang.Object arg0, V arg1, How to use getOrDefault method of HashMap?, Usage of HashMap.getOrDefault, HashMap.getOrDefault() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println("Map hashcode = " + map.hashCode());
// Remove element & then check hashcode. Hashcode of map changes as per
// elements.
map.remove("three");
System.out.println("Map after removeal of three = " + map);
System.out.println("Map hashcode = " + map.hashCode());
Output: Map = {one=uno, two=dos, three=tres} Map hashcode = 111577030 Map after removeal of three = {one=uno, two=dos} Map hashcode = 45460
Tag: Example for hashCode method of class java.util.AbstractMap., AbstractMap hashCode function example , How to use hashCode method of AbstractMap?, Usage of AbstractMap.hashCode, AbstractMap.hashCode() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
System.out.println("Is map empty = " + map.isEmpty());
// Clear map & check isEmpty
map.clear();
System.out.println("Is map empty = " + map.isEmpty());
HashMap<String, String> mapWithNullEntry = new HashMap<>();
mapWithNullEntry.put(null, null);
System.out.println("Is map empty for mapWithNullEntry = " + mapWithNullEntry.isEmpty());
// New map instance isEmpty
System.out.println("Is map empty of new map = " + new HashMap<>().isEmpty());
Output: Map = {one=uno, two=dos, three=tres} Is map empty = false Is map empty = true Is map empty for mapWithNullEntry = false Is map empty of new map = true
Tag: Example for isEmpty method of class java.util.HashMap., HashMap isEmpty function example , How to use isEmpty method of HashMap?, Usage of HashMap.isEmpty, HashMap.isEmpty() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
System.out.println("Map = " + map);
// For each on map key set
map.keySet().forEach(k -> System.out.println("Key: " + k + " Value: " + map.get(k)));
// Remove entry using key set removeIf
map.keySet().removeIf(k -> "two".equals(k));
System.out.println("Map after remove two = " + map);
// Remove entry using key set remove key
map.keySet().remove("three");
System.out.println("Map after remove three = " + map);
Output: Map = {one=uno, two=dos, three=tres} Key: one Value: uno Key: two Value: dos Key: three Value: tres Map after remove two = {one=uno, three=tres} Map after remove three = {one=uno}
Tag: Example for keySet method of class java.util.HashMap., HashMap keySet function example , How to use keySet method of HashMap?, Usage of HashMap.keySet, HashMap.keySet() examples
HashMap<String, String> map = new HashMap<>();
map.put("one", "uno");
map.put("two", "dos");
map.put("three", "tres");
map.put("four", null);
System.out.println("Map = " + map);
// Merge value for existing key "two". If present then take new value & concat
// to existing value.
map.merge("two", "II", String::concat);
System.out.println("Map after merge for two = " + map);
// Merge for existing key with null value. Since key not present, just use new
// value directly without executing remapping function.
map.merge("four", "IV", (k, v) -> "F O U R");
System.out.println("Map Map after merge for four = " + map);
// Merge for non existing key
map.merge("five", "V", String::concat);
System.out.println("Map Map after merge for fivex = " + map);
Output: Map = {four=null, one=uno, two=dos, three=tres} Map after merge for two = {four=null, one=uno, two=dosII, three=tres} Map Map after merge for four = {four=IV, one=uno, two=dosII, three=tres} Map Map after merge for fivex = {four=IV, one=uno, five=V, two=dosII, three=tres}
Tag: Example for merge method of class java.util.HashMap., HashMap merge function example with arguments K key, V value, java.util.function.BiFunction<? super V, ? super V, ? extends V> remappingFunction, How to use merge method of HashMap?, Usage of HashMap.merge, HashMap.merge() examples
Import statements used in examples.
java.util.ArrayList
java.util.HashMap
java.util.function.BiFunction
java.util.function.Function
java.util.function.BiConsumer
java.util.Map
java.util.TreeMap
Tag: Simple working examples of methods / functions of class java.util.HashMap along with their console output, java.util.HashMap tutorial., Guide to java.util.HashMap & its methods., Understanding java.util.HashMap with examples.