Il metodo computeIfAbsent () di Java HashMap calcola un nuovo valore e lo associa alla chiave specificata se la chiave non è associata a nessun valore nella hashmap.
La sintassi del computeIfAbsent()
metodo è:
hashmap.computeIfAbsent(K key, Function remappingFunction)
In questo caso, hashmap è un oggetto della HashMap
classe.
parametri computeIfAbsent ()
Il computeIfAbsent()
metodo richiede 2 parametri:
- chiave - chiave a cui associare il valore calcolato
- remappingFunction - funzione che calcola il nuovo valore per la chiave specificata
Nota : la funzione di rimappatura non può accettare due argomenti.
computeIfAbsent () Valore restituito
- restituisce il nuovo o il vecchio valore associato alla chiave specificata
- restituisce
null
se nessun valore è associato alla chiave
Nota : se si ottiene remappingFunction null
, la mappatura per la chiave specificata viene rimossa.
Esempio 1: Java HashMap computeIfAbsent ()
import java.util.HashMap; class Main ( public static void main(String() args) ( // create an HashMap HashMap prices = new HashMap(); // insert entries to the HashMap prices.put("Shoes", 200); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: " + prices); // compute the value of Shirt int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280); System.out.println("Price of Shirt: " + shirtPrice); // print updated HashMap System.out.println("Updated HashMap: " + prices); ) )
Produzione
HashMap: (Pantaloni = 150, Borsa = 300, Scarpe = 200) Prezzo della camicia: 280 HashMap aggiornata: (Pantaloni = 150, Camicia = 280, Borsa = 300, Scarpe = 200)
Nell'esempio sopra, abbiamo creato una hashmap denominata prezzi. Notare l'espressione,
prices.computeIfAbsent("Shirt", key -> 280)
Qui,
- key -> 280 è un'espressione lambda. Restituisce il valore 280. Per ulteriori informazioni sull'espressione lambda, visitare Java Lambda Expressions.
- prices.computeIfAbsent () associa il nuovo valore restituito dall'espressione lambda alla mappatura per Shirt. È possibile solo perché Shirt non è già mappato su alcun valore nella hashmap.
Esempio 2: computeIfAbsent () se la chiave è già presente
import java.util.HashMap; class Main ( public static void main(String() args) ( // create an HashMap HashMap prices = new HashMap(); // insert entries to the HashMap prices.put("Shoes", 180); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: " + prices); // mapping for Shoes is already present // new value for Shoes is not computed int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280); System.out.println("Price of Shoes: " + shoePrice); // print updated HashMap System.out.println("Updated HashMap: " + prices); ) )
Produzione
HashMap: (Pant = 150, Bag = 300, Shoes = 180) Prezzo delle scarpe: 180 HashMap aggiornata: (Pant = 150, Bag = 300, Shoes = 180)
Nell'esempio sopra, la mappatura per Shoes è già presente nella hashmap. Pertanto, il computeIfAbsent()
metodo non calcola il nuovo valore per Shoes.
Lettura consigliata
- HashMap compute (): calcola il valore per la chiave specificata
- HashMap computeIfPresent () - calcola il valore se la chiave specificata è già mappata a un valore
- Java HashMap merge (): esegue la stessa attività di
compute()