Java教程

Java HashMap merge()

Java HashMap merge()

如果指定的键不存在,Java HashMap merge() 方法会将指定的键/值映射插入到哈希图中。
如果指定的键已经与一个值相关联,则该方法用指定函数的结果替换旧值。
merge() 方法的语法是:
hashmap.merge(key, value, remappingFunction)
这里, hashmapHashMap 类的对象。

merge() 参数

merge() 方法接受 3 个参数:
key-与指定的 value 关联的键 value-要与 key 关联的值,如果 key 已经与任何值关联 remappingFunction-如果 key 已经与值相关联,则结果与 key 相关联

merge() 返回值

返回与相关联的新值 如果没有与 key 关联的值,则返回 null
注意: 如果 remappingFunction 结果为 null,则删除指定键的映射。

示例 1: HashMap merge()以插入新条目

import java.util.HashMap;
class Main {
  public static void main(String[] args) {
    // create an HashMap
    HashMap<String, Integer> 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);
    int returnedValue = prices.merge("Shirt", 100, (oldValue, newValue)-> oldValue + newValue);
    System.out.println("Price of Shirt: " + returnedValue);
    // print updated HashMap
    System.out.println("Updated HashMap: " + prices);
  }
}
输出
HashMap: {Pant=150, Bag=300, Shoes=200}
Price of Shirt: 100
Updated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}
在上面的例子中,我们创建了一个名为 prices 的哈希图。注意表达式,
prices.merge("Shirt", 100, (oldValue, newValue)-> oldValue + newValue)
这里,我们使用了 lambda 表达式, (oldValue, newValue)-> oldValue + newValue) 作为重映射函数。要了解有关 lambda 表达式的更多信息,请访问 Java Lambda 表达式。
由于键 Shirt 不存在于 prices 中, merge() 方法插入映射 Shirt=100。并且,忽略重映射函数的结果。

示例 2: HashMap merge() 插入带有重复键的条目

import java.util.HashMap;
class Main {
  public static void main(String[] args) {
    // create an HashMap
    HashMap<String, String> countries = new HashMap<>();
    // insert entries to the HashMap
    countries.put("Washington", "America");
    countries.put("Canberra", "Australia");
    countries.put("Madrid", "Spain");
    System.out.println("HashMap: " + countries);
    // merge mapping for key Washington
    String returnedValue = countries.merge("Washington", "USA", (oldValue, newValue)-> oldValue + "/" + newValue);
    System.out.println("Washington: " + returnedValue);
    // print updated HashMap
    System.out.println("Updated HashMap: " + countries);
  }
}
输出
HashMap: {Madrid=Spain, Canberra=Australia, Washington=America}
Washington: America/USA
Updated HashMap: {Madrid=Spain, Canberra=Australia, Washington=America/USA}, 
在上面的例子中,我们创建了一个名为 countries 的哈希映射。注意表达式,
countries.merge("Washington", "USA", (oldValue, newValue)-> oldValue + "/" + newValue)
这里,我们使用了 lambda 表达式, (oldValue, newValue)-> oldValue + "/" + newValue) 作为重映射函数。
由于键 Washington 已经存在于 countries 中,旧值将替换为重新映射函数返回的值。因此, Washington 的映射包含值 America/USA

示例 3: HashMap merge() 合并两个 HashMap

import java.util.HashMap;
class Main {
  public static void main(String[] args) {
    // create an HashMap
    HashMap<String, Integer> prices1 = new HashMap<>();
    // insert entries to the HashMap
    prices1.put("Pant", 230);
    prices1.put("Shoes", 350);
    System.out.println("HashMap 1: " + prices1);
    // create another hashmap
    HashMap<String, Integer> prices2 = new HashMap<>();
    //insert entries to the HashMap
    prices2.put("Shirt", 150);
    prices2.put("Shoes", 320);
    System.out.println("HashMap 2: " + prices2);
    // forEach() access each entries of prices2
    // merge() inserts each entry from prices2 to prices1
    prices2.forEach((key, value)-> prices1.merge(key, value, (oldValue, newValue)-> {
      
      // return the smaller value
      if (oldValue < newValue) {
        return oldValue;
      }
      else {
        return newValue;
      }
    }));
    System.out.println("Merged HashMap: " + prices1);
  }
}
输出
HashMap 1: {Pant=230, Shoes=350}
HashMap 2: {Shirt=150, Shoes=320}
Merged HashMap: {Pant=230, Shirt=150, Shoes=320}
在上面的示例中,我们创建了两个名为 prices1prices2 的哈希映射。注意代码,
    prices2.forEach((key, value)-> prices1.merge(key, value, (oldValue, newValue)-> {
      if (oldValue < newValue) {
        return oldValue;
      }
      else {
        return newValue;
      }
    }));
这里,HashMap forEach() 方法访问哈希图的每个条目 price2 并将其合并到哈希图 prices1。我们使用了两个 lambda 表达式:
(key, value)->prices.merge(...)-它访问 prices1 的每个条目并将其传递给 merge() 方法。 (oldValue, newValue)-> {...}-这是一个重映射函数。它比较两个值并返回较小的值。
由于键 Shoes 存在于两个 hashmap 中, Shoes 的值被重映射函数的结果替换。

Java HashMap merge() 对比。 putAll

我们也可以使用 putAll() 方法来合并两个哈希图。但是,如果两个哈希图中都存在一个键,则旧值将替换为新值。
merge() 不同, putAll() 方法不提供重新映射功能。因此,我们无法决定为重复键存储什么值。
要了解有关 putAll() 方法的更多信息,请访问 Java HashMap putAll().
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4