In my earlier article, I had explained the Map.compute, Map.computeIfPresent and Map.computeIfAbsent methods added by Java 8. In addition,Java 8 has added two other methods called putIfAbsent and getOrDefault to the Map interface. In this article, I will be explaining the these methods.
Prior to Java 8, the Map interface had a put method. This method accepts a key and a value as a parameter and adds the key-value pair to the Map. If the key passed to this method is present in the Map, its value gets overwritten with the new value. However, sometimes, we may wish to add the key-value pair only if the key is absent from the Map. The putIfAbsent method caters to such situations. Thus, it accepts a key and a value as a parameter and adds the key-value pair to the Map only if the specified key is not present in the Map. If the key is present in the Map, it does not do anything. The following code demonstrates the put and putIfAbsent methods:
Map<Integer,String> shapesMap = new HashMap<>();
shapesMap.put(1, "circle");
shapesMap.put(2, "square");
shapesMap.put(3, "triangle");
shapesMap.put(1, "rectangle");
System.out.println("Value for key 1:"+shapesMap.get(1));
shapesMap.putIfAbsent(2, "pentagon");
System.out.println("Value for key 2:"+shapesMap.get(2));
shapesMap.putIfAbsent(4, "pentagon");
System.out.println("Value for key 4:"+shapesMap.get(4));
This code produces the following output:
Value for key 1:rectangle
Value for key 2:square
Value for key 4:pentagon
Prior to Java 8, the Map interface had a get method. This method accepts a key as a parameter and returns the value corresponding to the key. If the key passed to this method is not present in the Map, its returns a null. However, sometimes, we may wish to return a default value if the specified key is not present in the Map. The getOrDefault method caters to such situations. Thus, it accepts a key and a default value as a parameter. It returns the value corresponding to the key only if the key is present in the map. If the key is not present in the Map, it returns the specified default value. The following code demonstrates the get and getOrDefault methods:
Map<Integer,String> shapesMap = new HashMap<>();
shapesMap.put(1, "circle");
shapesMap.put(2, "square");
shapesMap.put(3, "triangle");
System.out.println("Value for key 1:"+shapesMap.get(1));
System.out.println("Value for key 5:"+shapesMap.get(5));
System.out.println("Value for key 2:"+shapesMap.getOrDefault(2,"pentagon"));
System.out.println("Value for key 5:"+shapesMap.getOrDefault(5,"pentagon"));
This code produces the following output:
Value for key 1:circle
Value for key 5:null
Value for key 2:square
Value for key 5:pentagon
So, in this article, we saw how the putIfAbsent and getOrDefault methods work. Just to summarize, the putIfAbsent adds the a key-value pair to the Map only if the key is absent in the Map. The getOrDefault returns the value corresponding to a key if the key is present in the Map. Otherwise, it returns a default value.