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