Java Program to Convert HashMap into List
What is HashMap in Java ?
A HashMap in Java is a class that implements the Map interface and stores key-value pairs in a hash table. The keys must be unique, and the values can be any object. The class uses a technique called hashing to efficiently map keys to their corresponding values, which allows for fast lookups and insertion of elements. It is a part of the Java Collections Framework.
What is List in Java ?
A List in Java is an ordered collection of elements that allows duplicate values. It is an interface in the Java Collections Framework and is implemented by classes such as ArrayList and LinkedList. Lists allow for adding, removing, and accessing elements by index and also provide various methods for searching and sorting. Lists are useful for cases where the order of elements is important and/or duplicate elements are allowed.
There are several ways to convert a HashMap into a List in Java :
1.Using the entrySet() method: This method returns a set of the map’s key-value pairs, which can then be converted to a list using the Arrays.asList() method.
HashMap<Integer, String> map = new HashMap<>(); // add elements to the map List<Map.Entry<Integer, String>> list = new ArrayList<>(map.entrySet());
2.Using the keySet() method: This method returns a set of the map’s keys, which can then be converted to a list using the Arrays.asList() method.
HashMap<Integer, String> map = new HashMap<>(); // add elements to the map List list = new ArrayList<>(map.keySet());
3.Using the values() method: This method returns a collection of the map’s values, which can then be converted to a list using the Arrays.asList() method.
HashMap<Integer, String> map = new HashMap<>(); // add elements to the map List list = new ArrayList<>(map.values());
4.Using a for-each loop: Iterate over the map’s key-value pairs using a for-each loop and add the keys or values to a list.
HashMap<Integer, String> map = new HashMap<>(); // add elements to the map List keyList = new ArrayList<>(); for (Map.Entry<Integer, String> entry : map.entrySet()) { keyList.add(entry.getKey()); }
5.Using the Stream API: Java 8 introduced the Stream API which can be used to convert a HashMap into a List.
HashMap<Integer, String> map = new HashMap<>(); // add elements to the map List<Map.Entry<Integer, String>> list = map.entrySet().stream().collect(Collectors.toList());
Example 1 :
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "apple");
map.put(2, "banana");
map.put(3, "orange");
// Using the entrySet() method
List<Map.Entry<Integer, String>> entryList = new ArrayList<>(map.entrySet());
System.out.println("Map converted to List using entrySet(): " + entryList);
// Using the keySet() method
List<Integer> keyList = new ArrayList<>(map.keySet());
System.out.println("Map converted to List using keySet(): " + keyList);
// Using the values() method
List<String> valueList = new ArrayList<>(map.values());
System.out.println("Map converted to List using values(): " + valueList);
}
}
Output :
Map converted to List using entrySet(): [1=apple, 2=banana, 3=orange] Map converted to List using keySet(): [1, 2, 3] Map converted to List using values(): [apple, banana, orange]
Explanation:
- In the code, we are creating a HashMap with 3 key-value pairs.
- Using the entrySet() method, we are creating a new ArrayList using the entrySet method of the map
- which returns a set of the map’s key-value pairs, then we are adding this set to the list.
- Using the keySet() method, we are creating a new ArrayList using the keySet method of the map which returns a set of the map’s keys, then we are adding this set to the list.
- Using the values() method, we are creating a new ArrayList using the values method of the map which returns a collection of the map’s values, then we are adding this collection to the list.
- Finally, we are printing all the list, which contains keys, values and key-value pairs respectively in the order they were added to the map.
Example 2 :
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "apple");
map.put(2, "banana");
map.put(3, "orange");
// Using a for-each loop
List<Integer> keyList = new ArrayList<>();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
keyList.add(entry.getKey());
}
System.out.println("Map converted to List using for-each loop: " + keyList);
// Using the Stream API
List<Map.Entry<Integer, String>> entryList = map.entrySet().stream().collect(Collectors.toList());
System.out.println("Map converted to List using Stream API: " + entryList);
}
}
Output :
Map converted to List using for-each loop: [1, 2, 3] Map converted to List using Stream API: [1=apple, 2=banana, 3=orange]
Explanation:
- In the code, we are creating a HashMap with 3 key-value pairs.
- Using a for-each loop, we are iterating over the map’s key-value pairs and adding the keys to a list.
- Using the Stream API, we are converting the map’s entrySet to a stream and then collecting the stream into a List.
- Finally, we are printing both the lists, one contains the keys and the other one contains key-value pairs respectively in the order they were added to the map.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment