Creating HashMap from Other Maps in Java
Last Updated :
15 Nov, 2021
Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys.
There are three main types of maps in java
- HashMap
- LinkedHashMap
- TreeMap
These interfaces extend the Map interface.
There are various ways to convert one map to another:
- Using iterator/a loop
- Using constructor
- Using putAll() method
Method 1: Using iterator/a loop
Iterate each element of the map (LinkedHashMap in this case) and add each one of them in the new HashMap.
Java
// Java program for creating HashMap from Other Maps
import java.util.*;
public class to_hashmap {
public static void main(String a[])
{
// create an instance of LinkedHashMap
LinkedHashMap<String, String> lhm
= new LinkedHashMap<String, String>();
// Add mappings using put method
lhm.put("Apurva", "Bhatt");
lhm.put("James", "Bond");
lhm.put("Scarlett ", "Johansson");
// It prints the elements in same order
// as they were inserted
System.out.println(lhm);
Map<String, String> gfg = new HashMap<String, String>();
// Using entrySet() method create a set out of the same elements
// contained in the hash map
for (Map.Entry<String, String> entry : lhm.entrySet())
gfg.put(entry.getKey(), entry.getValue());
System.out.println(gfg);
}
}
Output{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{James=Bond, Apurva=Bhatt, Scarlett =Johansson}
Method 2: Using constructor
Pass the given Map (TreeMap in this case) to the HashMap constructor--it will automatically take care of converting the given Map to HashMap.
Java
// Java program for creating HashMap from Other Maps
// using constructor
import java.util.*;
public class to_hashmap {
public static void main(String a[])
{
// create an instance of TreeMap
Map<String, String> tm = new TreeMap<String, String>();
// Add mappings using put method
tm.put("Apurva", "Bhatt");
tm.put("James", "Bond");
tm.put("Scarlett ", "Johansson");
// It prints the elements in same order
// as they were inserted
System.out.println(tm);
Map<String, String> gfg = new HashMap<String, String>(tm);
System.out.println(gfg);
}
}
Output{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
Method 3: Using putAll() method
It is similar to the previous method, rather than passing the given Map to HashMap constructor, pass it to the putAll() method and it will automatically convert it to the HashMap.
Java
// Java program for creating HashMap from Other Maps
// using putAll() method
import java.util.*;
public class two_hashmap {
public static void main(String a[])
{
// create an instance of TreeMap
Map<String, String> tm = new TreeMap<String, String>();
// Add mappings using put method
tm.put("Apurva", "Bhatt");
tm.put("James", "Bond");
tm.put("Scarlett ", "Johansson");
// It prints the elements in same order
// as they were inserted
System.out.println(tm);
Map<String, String> gfg = new HashMap<String, String>();
// using put all command
gfg.putAll(tm);
System.out.println(gfg);
}
}
Output{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
{Apurva=Bhatt, James=Bond, Scarlett =Johansson}
Similar Reads
Removing all Mapping From HashMap in Java Maps are used for when you want to associate a key with a value and Lists are an ordered collection. Map is an interface in the Java Collection Framework and a HashMap is one implementation of the Map interface. HashMap is efficient for locating a value based on a key and inserting and deleting valu
4 min read
Getting Synchronized Map from Java HashMap HashMap is a non synchronized collection class. If we want to perform thread-safe operations on it then we must have to synchronize it explicitly. In order to synchronize it explicitly the synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map back
2 min read
Convert HashMap to LinkedList in Java HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. LinkedList is a part of the Collection framework present in java.util package. This class is an implementa
2 min read
Program to Convert HashMap to TreeMap in Java HashMap is a part of Javaâs collection since Java 1.2. It provides the basic implementation of Map interface of Java which stores the data in (Key, Value) pairs. To access a value in HashMap, one must know its key. HashMap is known as HashMap because it uses a technique Hashing for storage of data.
6 min read
Traverse Through a HashMap in Java HashMap stores the data in (Key, Value) pairs, and you can access them by an index of another type. HashMap class implements Map interface which allows us to store key. hashMap is a part of the java collections framework been up since Java 1.2. It internally uses hashing technique which is pretty fa
4 min read
How to Merge Two HashMaps in Java while Handling Conflicts? HashMap is a part of the collection framework found in Java. It is found in the java.util package and provides the basic implementation of the Map interface of Java. Conflict handling is very important in HashMap as if they are ignored the code will not work properly. In this article, we will learn
3 min read
How to Copy One HashMap to Another HashMap in Java? HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to
4 min read
Converting ArrayList to HashMap in Java 8 using a Lambda Expression A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap. Syntax: (parms1, parms2) -> expressionExamples: Input : List : [1="1", 2="2", 3="3"] Output: Map : {1=1,
2 min read
How to Convert Two Arrays Containing Keys and Values to HashMap in Java? HashMap is part of the Collections framework of java. It stores the data in the form of key-value pairs. These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java. T
2 min read