The Java HashMap compute() method is used to compute a new value for the specified key.
Syntax of compute() method
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
The parameters are-
- key- Key with which the computed value has to be mapped.
- remappingFunction- It is an expression of type BiFunction functional interface used to compute a value.
Method returns a new value associated with the specified key or null if there is no new values. If the remapping function returns null, the mapping is removed (or remains absent if initially absent).
compute() method Java examples
1. In this example a new value is computed for the specified key. For the example a HashMap is created with product name as key and price as value. Then compute() is used to change the price (value) for the specified key.
import java.util.HashMap; import java.util.Map; public class MapComputeDemo { public static void main(String[] args) { Map<String, Double> products = new HashMap<>(); products.put("Laptop", 1200.0); products.put("RAM", 60.50); products.put("USB", 10.45); products.put("Mouse", 15.0); System.out.println("*** Initial Values ***"); System.out.println(products); // Laptop at 20% discount products.compute("Laptop", (k, v)-> v- (v*20/100)); System.out.println("*** After Compute ***"); System.out.println(products); } }Output
*** Initial Values *** {Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5} *** After Compute *** {Laptop=960.0, Mouse=15.0, USB=10.45, RAM=60.5}
2. In this example we’ll see what happens if the remappingFunction returns null. In that case the (key, value) pair should be removed. To verify that the function explicitly returns null in the code.
public class MapComputeDemo { public static void main(String[] args) { Map<String, Double> products = new HashMap<>(); products.put("Laptop", 1200.0); products.put("RAM", 60.50); products.put("USB", 10.45); products.put("Mouse", 15.0); System.out.println("*** Initial Values ***"); System.out.println(products); // remappingFunction returns null products.compute("Laptop", (k, v)-> null); System.out.println("*** After Compute ***"); System.out.println(products); } }Output
*** Initial Values *** {Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5} *** After Compute *** {Mouse=15.0, USB=10.45, RAM=60.5}
As you can see the product “Laptop” is removed.
3. Using compute() method to compute all values in the HashMap. In the products HashMap if you want to increase price by 10% for all the products.
public class MapComputeDemo { public static void main(String[] args) { Map<String, Double> products = new HashMap<>(); products.put("Laptop", 1200.0); products.put("RAM", 60.50); products.put("USB", 10.45); products.put("Mouse", 15.0); System.out.println("*** Initial Values ***"); System.out.println(products); products.forEach((k,v) -> products.compute(k, (key, value)-> value + (value*10/100))); System.out.println("*** After Compute ***"); System.out.println(products); } }Output
*** Initial Values *** {Laptop=1200.0, Mouse=15.0, USB=10.45, RAM=60.5} *** After Compute *** {Laptop=1320.0, Mouse=16.5, USB=11.495, RAM=66.55}
That's all for the topic Java HashMap compute() With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Map#putIfAbsent() in Java With Examples
- Java HashMap computeIfAbsent() With Examples
- Java LinkedHashMap With Examples
- Convert Array to ArrayList in Java
- HashSet Internal Implementation in Java
- Daemon Thread in Java
- Java Stream count() With Examples
- Merge Sort Java Program
- Spring @Component, @Service, @Repository, @Controller Annotations
- Setting Error Page (404 Page) in React Router
No comments:
Post a Comment