HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. Unlike Hashmap, an ArrayList is used to provides us with dynamic arrays in Java. It is an alternative to an array that provides a lot more flexibility in terms of storage. In this article, we will see how to convert a Hashmap to an ArrayList.
Since every element in the hashmap contains a key/value pair, it is not possible to store this key/value pair in a single ArrayList. Therefore, in order to convert a hashmap into an ArrayList, we need to maintain two separate ArrayList’s where one ArrayList is used to store the Keys and the other to store the values corresponding to those keys. The following are the various methods used to convert a hashmap to the ArrayList.
Method 1:
One way to convert is to use the constructor of the ArrayList. In order to do this, we can use the keySet() method present in the HashMap. This method returns the set containing all the keys of the hashmap. This set can be passed into the ArrayList while initialization in order to obtain an ArrayList containing all the keys. After obtaining the keys, we can use the values() method present in the hashmap to obtain a collection of all the values present in the hashmap. We can use this collection to initialize another array that contains all the values for the keys in the hashmap.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
HashMap<String, Integer> map
= new HashMap<>();
map.put( "vishal" , 10 );
map.put( "sachin" , 30 );
map.put( "vaibhav" , 20 );
Set<String> keySet = map.keySet();
ArrayList<String> listOfKeys
= new ArrayList<String>(keySet);
Collection<Integer> values = map.values();
ArrayList<Integer> listOfValues
= new ArrayList<>(values);
System.out.println( "The Keys of the Map are "
+ listOfKeys);
System.out.println( "The Values of the Map are "
+ listOfValues);
}
}
|
Output:
The Keys of the Map are [vaibhav, vishal, sachin]
The Values of the Map are [20, 10, 30]
Note: In the above example, it can be clearly observed that the correlation between the keys and the values is retained by the index of the ArrayList. This means that the value for the specific key can be found at the same index in the values list.
Method 2:
Another way to convert a HashMap into an ArrayList is to use the Stream API to convert map keys and values to respective lists.
Java
import java.util.*;
import java.util.stream.*;
public class GFG {
public static void main(String[] args)
{
HashMap<String, Integer> map
= new HashMap<>();
map.put( "vishal" , 10 );
map.put( "sachin" , 30 );
map.put( "vaibhav" , 20 );
ArrayList<String> listOfKeys
= map.keySet().stream().collect(
Collectors.toCollection(ArrayList:: new ));
ArrayList<Integer> listOfValues
= map.values().stream().collect(
Collectors.toCollection(ArrayList:: new ));
System.out.println( "The Keys of the Map are "
+ listOfKeys);
System.out.println( "The Values of the Map are "
+ listOfValues);
}
}
|
Output:
The Keys of the Map are [vaibhav, vishal, sachin]
The Values of the Map are [20, 10, 30]
Note: The Collectors.toCollection(ArrayList::new) passed to the collect() method to collect as new ArrayList. We can also use toList() instead of this function to convert it to a List and then to any other List implementations.&
Method 3:
In both the above methods, the HashMap is converted to an ArrayList without retaining the direct key/value relationship. The keys and values are stored in two different ArrayLists. However, we can retain the key/value pairs in the ArrayList using the entrySet() method. This method is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map or we can create a new set and store the map elements into them. We can pass this Set into the ArrayList using the constructor in order to form an ArrayList with the Entry objects as the key/value pairs.
Example:
Java
import java.util.*;
import java.util.stream.*;
import java.util.Map.*;
public class GFG {
public static void main(String[] args)
{
HashMap<String, Integer> map
= new HashMap<>();
map.put( "vishal" , 10 );
map.put( "sachin" , 30 );
map.put( "vaibhav" , 20 );
Set<Map.Entry<String, Integer> > entrySet
= map.entrySet();
ArrayList<Map.Entry<String, Integer> > listOfEntry
= new ArrayList<Entry<String, Integer> >(entrySet);
System.out.println(listOfEntry);
}
}
|
Output:
[vaibhav=20, vishal=10, sachin=30]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!