Java HashMap computeIfAbsent() メソッド
computeIfAbsent() メソッドは、hashMap 内の指定されたキーの値を再計算します。このキーが存在しない場合は、hashMap に追加します。
computeIfAbsent() メソッドの構文は次のとおりです:
hashmap.computeIfAbsent(K key, Function remappingFunction)
注:hashmap は HashMap クラスのオブジェクトです。
パラメータの説明:
- key - キー
- remappingFunction - 再マッピング関数。値を再計算するために使用されます。
戻り値
key に対応する value が存在しない場合は、remappingFunction によって再計算された値を取得して、その key の value として保存します。それ以外の場合は value を返します。
例
以下の例は、computeIfAbsent() メソッドの使用法を示しています:
例
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);
// Shirt の値を計算する
int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280);
System.out.println("Price of Shirt: " + shirtPrice);
// 更新後の 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);
// Shirt の値を計算する
int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280);
System.out.println("Price of Shirt: " + shirtPrice);
// 更新後の HashMap を出力する
System.out.println("Updated HashMap: " + prices);
}
}
上記のプログラムを実行すると、出力結果は次のようになります:
HashMap: {Pant=150, Bag=300, Shoes=200}
Price of Shirt: 280
Updated HashMap: {Pant=150, Shirt=280, Bag=300, Shoes=200}
上記の例では、prices という名前の HashMap を作成しました。
次の式に注意してください:
prices.computeIfAbsent("Shirt", key -> 280)
コードでは、匿名関数として lambda 式を使用しました。key-> 280再マッピング関数として、prices.computeIfAbsent() は lambda 式が返す新しい値を Shirt に関連付けます。
Shirt は HashMap に存在しないため、key/value ペアが新しく追加されました。
lambda 式の詳細については、以下を参照してください:Java ラムダ式。
key がすでに存在する場合:
例
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// HashMap を作成する
HashMap<String, Integer> prices = new HashMap<>();
// HashMap にマッピング関係を追加する
prices.put("Shoes", 180);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
// Shoes のマッピング関係はすでに存在する
// Shoes の新しい値は計算されない
int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280);
System.out.println("Price of Shoes: " + shoePrice);
// 更新後の 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", 180);
prices.put("Bag", 300);
prices.put("Pant", 150);
System.out.println("HashMap: " + prices);
// Shoes のマッピング関係はすでに存在する
// Shoes の新しい値は計算されない
int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 280);
System.out.println("Price of Shoes: " + shoePrice);
// 更新後の HashMap を出力する
System.out.println("Updated HashMap: " + prices);
}
}
上記のプログラムを実行すると、出力結果は次のようになります:
HashMap: {Pant=150, Bag=300, Shoes=180}
Price of Shoes: 180
Updated HashMap: {Pant=150, Bag=300, Shoes=180}上記の例では、Shoes のマッピング関係は HashMap にすでに存在するため、Shoes の新しい値は計算されません。
その他の拡張
Java HashMap