Java HashMap computeIfAbsent() Method



Description

The Java HashMap computeIfAbsent() method is used to compute a mapping for the specified key if the specified key is not already associated with a value (or is mapped to null), using the given mapping function and enters it into this map unless null.

Declaration

Following is the declaration for java.util.HashMap.computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) method.

public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

Parameters

key − key with which the specified value is to be associated

remappingFunction − the remapping function to compute a value

Return Value

The method call returns the new value associated with the specified key, or null if none.

Exception

ConcurrentModificationException − if it is detected that the remapping function modified this map.

Compute Mapping of unmapped key of a HashMap of Integer, Integer Pair Example

The following example shows the usage of Java HashMap computeIfAbsent() method to get updated values of a Map. We've created two Map objects of Integer,Integer pair. Then few entries are added to map, then using computeIfAbsent method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Integer> newmap = new HashMap<>();

      // populate map
      newmap.put(1, 1);
      newmap.put(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> key);      

      System.out.println("Updated Map: " + newmap);   
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Map: {1=1, 3=3}
Updated Map: {1=1, 2=2, 3=3}

Compute Mapping of unmapped key of a HashMap of Integer, String Pair Example

The following example shows the usage of Java HashMap computeIfAbsent() method to get updated values of a Map. We've created two Map objects of Integer,String pair. Then few entries are added to map, then using computeIfAbsent() method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, String> newmap = new HashMap<>();

      // populate map
      newmap.put(1, "A");
      newmap.put(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> {return "B";});

      System.out.println("Updated Map: " + newmap);   
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Map: {1=A, 3=C}
Updated Map: {1=A, 2=B, 3=C}

Compute Mapping of unmapped key of a HashMap of Integer, Student Pair Example

The following example shows the usage of Java HashMap computeIfAbsent() method to get a update values of a Map. We've created two Map objects of Integer,Student. Then few entries are added to map, then using computeIfAbsent() method, the values are updated and then updated map is printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Student> newmap = new HashMap<>();

      // populate map
      newmap.put(1, new Student(1, "Julie"));      
      newmap.put(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> {return new Student(2, "Robert");});
      
      System.out.println("Updated Map: " + newmap);   
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   
   public Student update(String surname) {
	   this.name = this.name.concat(" " + surname);
	   return this;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Map: {1=[ 1, Julie ], 3=[ 3, Adam ]}
Updated Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
java_util_hashmap.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy