Java HashMap computeIfAbsent () yöntemi yeni bir değer hesaplar ve anahtar karma haritadaki herhangi bir değerle ilişkilendirilmemişse bunu belirtilen anahtarla ilişkilendirir.
computeIfAbsent()
Yöntemin sözdizimi şöyledir:
hashmap.computeIfAbsent(K key, Function remappingFunction)
Burada hashmap, HashMap
sınıfın bir nesnesidir .
computeIfAbsent () Parametreleri
computeIfAbsent()
Yöntem 2 parametreleri alır:
- anahtar - hesaplanan değerin ilişkilendirileceği anahtar
- remappingFunction - belirtilen anahtar için yeni değeri hesaplayan işlev
Not : Yeniden eşleme işlevi iki bağımsız değişken alamaz.
computeIfAbsent () Dönüş Değeri
- belirtilen anahtarla ilişkili yeni veya eski değeri döndürür
null
anahtarla ilişkili bir değer yoksa döndürür
Not : remappingFunction sonuçlanırsa null
, belirtilen anahtar için eşleme kaldırılır.
Örnek 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); ) )
Çıktı
HashMap: (Pantolon = 150, Çanta = 300, Ayakkabı = 200) Gömlek Fiyatı: 280 Güncellenmiş HashMap: (Pantolon = 150, Gömlek = 280, Çanta = 300, Ayakkabı = 200)
Yukarıdaki örnekte, fiyatlar adlı bir hashmap oluşturduk. İfadeye dikkat edin,
prices.computeIfAbsent("Shirt", key -> 280)
Buraya,
- anahtar -> 280 bir lambda ifadesidir. 280 değerini döndürür. Lambda ifadesi hakkında daha fazla bilgi edinmek için Java Lambda İfadeleri sayfasını ziyaret edin.
- fiyatları.computeIfAbsent () , lambda ifadesi tarafından döndürülen yeni değeri Shirt eşlemesiyle ilişkilendirir. Bu yalnızca, Shirt'ün hashmap'teki herhangi bir değere zaten eşlenmemiş olması nedeniyle mümkündür.
Örnek 2: anahtar zaten mevcutsa 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", 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); ) )
Çıktı
HashMap: (Pantolon = 150, Çanta = 300, Ayakkabı = 180) Ayakkabı Fiyatı: 180 Güncellenmiş HashMap: (Pantolon = 150, Çanta = 300, Ayakkabı = 180)
Yukarıdaki örnekte, Shoes için eşleme hashmap'de zaten mevcuttur. Bu nedenle, computeIfAbsent()
yöntem Ayakkabılar için yeni değeri hesaplamaz.
Önerilen Kaynaklar
- HashMap compute () - belirtilen anahtarın değerini hesaplar
- HashMap computeIfPresent () - belirtilen anahtar zaten bir değere eşlenmişse değeri hesaplar
- Java HashMap merge () - ile aynı görevi gerçekleştirir
compute()