Java 程序从一个 Map 复制所有键值对到另一个 Map 中
要复制,请使用 putAll() 方法。
我们首先创建两个 Map −
第一个 Map −
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));第二个 Map −
HashMap hm2 = new HashMap();
hm.put("Bag", new Integer(1100));
hm.put("Sunglasses", new Integer(2000));
hm.put("Frames", new Integer(800));现在,将键值对从一个 Map 复制到另一个 Map −
hm.putAll(hm2);
以下是一个从一个 Map 复制所有键值对到另一个 Map 的示例 −
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
// Create hash map 1
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
System.out.println("Map1 = "+hm);
// Create hash map 2
HashMap hm2 = new HashMap();
hm.put("Bag", new Integer(1100));
hm.put("Sunglasses", new Integer(2000));
hm.put("Frames", new Integer(800));
hm.putAll(hm2);
System.out.println("Map1 after copying values of Map2 = "+hm);
}
}输出
Map1 = {Belt=600, Wallet=700}
Map1 after copying values of Map2 = {Frames=800, Belt=600, Wallet=700, Bag=1100, Sunglasses=2000}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP