Java HashMap compute()
[Java HashMap] Java HashMap
compute() hashMap key
compute()
hashmap.compute(K key, BiFunction remappingFunction)
hashmap HashMap
- key -
- remappingFunction -
key value null remappingFunction
compute()
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// HashMap
HashMap<String, Integer> prices = new HashMap<>();
// HashMap
prices.put("Shoes", 200);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
// 10%
int newPrice = prices.compute("Shoes", (key, value) -> value - value * 10/100);
System.out.println("Discounted Price of Shoes: " + newPrice);
// HashMap
System.out.println("Updated HashMap: " + prices);
}
}
class Main {
public static void main(String[] args) {
// HashMap
HashMap<String, Integer> prices = new HashMap<>();
// HashMap
prices.put("Shoes", 200);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
// 10%
int newPrice = prices.compute("Shoes", (key, value) -> value - value * 10/100);
System.out.println("Discounted Price of Shoes: " + newPrice);
// HashMap
System.out.println("Updated HashMap: " + prices);
}
}
HashMap: {Pant=150, Bag=300, Shoes=200}
Discounted Price of Shoes: 180
Updated HashMap: {Pant=150, Bag=300, Shoes=180
prices HashMap
prices.compute("Shoes", (key, value) -> value - value * 10/100)
lambda (key, value) -> value - value * 10/100
lambda Java Lambda
[Java HashMap] Java HashMap