In this article, we will add one HashMap contents to another HashMap using putAll() method of Map interface
Adding one HashSet to another HashSet :
Method signature:
void putAll(Map<? extends K, ? extends V> map);
- This method is used to add one HashMap contents to another HashMap contents
AddOneHashMapToAnotherHashMap.java
package in.bench.resources.java.map;
import java.util.HashMap;
public class AddOneHashMapToAnotherHashMap {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
HashMap<String, String> hashMap1 = new HashMap<String, String>();
// adding key-value pairs to 1st HashMap object
hashMap1.put("Google", "Sundar Pichai");
hashMap1.put("Apple", "Steve Jobs");
hashMap1.put("Amazon", "Jeff Bezos");
// Original HashMap-1 contents
System.out.println("Original HashMap-1 contents: \n"
+ hashMap1);
// creating HashMap object of type <String, String>
HashMap<String, String> hashMap2 = new HashMap<String, String>();
// adding key-value pairs to 2nd HashMap object
hashMap2.put("Microsoft", "Bill Gates");
hashMap2.put("Whatsup", "Brian Acton");
// HashMap-2 contents
System.out.println("\n\nHashMap-2 contents: \n"
+ hashMap2);
// put all hashMap2 contents into original hashMap1
hashMap1.putAll(hashMap2);
// Modified HashMap-1 contents
System.out.println("\n\nModified HashMap-1 contents: \n"
+ hashMap1);
}
}
Output:
Original HashMap-1 contents:
{Google=Sundar Pichai, Apple=Steve Jobs, Amazon=Jeff Bezos}
HashMap-2 contents:
{Microsoft=Bill Gates, Whatsup=Brian Acton}
Modified HashMap-1 contents:
{Google=Sundar Pichai, Microsoft=Bill Gates, Apple=Steve Jobs,
Amazon=Jeff Bezos, Whatsup=Brian Acton}
Related Articles:
- Java – How to get all keys of a HashMap ?
- Java – How to get all values of a HashMap ?
- Java – How to get all Entries or Key-Value pairs of HashMap ?
- Java – How to get size or length of HashMap ?
- Java – How to check whether a particular key is present in HashMap ?
- Java – How to check whether a particular value is present in HashMap ?
- Java – How to check whether HashMap is empty or not ?.
- Java – Adding one HashMap to another HashMap using putAll method
- Java – How to delete an entry of HashMap ?
- Java – How to delete all entries of HashMap ?
- Java 8 – How to remove an entry from HashMap by comparing values
- Java 8 – How to remove an entry from HashMap by comparing keys
- Java – Various ways to iterate through HashMap
- Java – To reverse order the LinkedHashMap contents
- Java – How ConcurrentModificationException can be handled ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!