How to Convert HashMap to List
HashMap is a class in Java that implements Map interface.It is an unsorted and unordered map whereas ArrayList implements List interface and maintains insertion order.Here we will try to convert a HashMap into an ArrayList.
Let us define a HashMap first.
Map<String, String> map = new HashMap<String, String>();
At first,let us see how to get list of keys from a HashMap.There is an implicit method defined in java library to get the list of keys of a HashMap.And after getting the list of keys, construct an ArrayList with it.
Set<String> keys = map.keySet(); //list of all the keys defined in Hashmap ArrayList<String> listOfKeys = new ArrayList<String>(keys);
Similarly, extract the values of a HashMap and construct an ArrayList out of it as below:
Collection<String> values = map.values(); //list of all the values defined in Hashmap ArrayList<String< listOfValues = new ArrayList<String>(values);
Other Interesting Posts Hello World Java Program Breakup Serialization and Deserialization in Java with Example Random password Generator in Java Convert HashMap to List in Java Sorting HashMap by Key and Value in Java Spring Boot Security Redirect after Login with complete JavaConfig Websocket spring Boot Integration with complete JavaConfig Spring MVC Angularjs Integration with complete JavaConfig Spring Hibernate Integration with complete JavaConfig
Now let us constuct a single ArrayList having both the keys and values of a HashMap in a single shot.
Set<Entry<String, String>> entrySet = map.entrySet(); ArrayList<Entry<String, String>> listOfEntries = new ArrayList<Entry<String, String>>(entrySet); for (Entry<String, String> entry : listOfEntries) { System.out.println(entry.getKey() + " : " + entry.getValue()); }
Conclusion
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
If You Appreciate This, You Can Consider:
- We are thankful for your never ending support.
About The Author
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.
Further Reading on Core Java
1. Hashmap Hashtable And Concurrenthashmap
2. Sorting Hashmap By Key And Value
3. Why Wait Notify Notifyall Defined In Object Class
4. Comparable And Comparator In Java With Example
5. Random Password Generator Java
6. Serialization And Deserialization Java Example
7. Hello World Java Program Breakup
8. Linkedlist Implementation Java
11. Pros And Cons Collection Java
Recommended
- Hashmap Hashtable And Concurrenthashmap
- Sorting Hashmap By Key And Value
- Why Wait Notify Notifyall Defined In Object Class
- Comparable And Comparator In Java With Example
- Random Password Generator Java
- Serialization And Deserialization Java Example
- Hello World Java Program Breakup
- Linkedlist Implementation Java
- Stack Implementation Java
- Tree Implementation Java
- Pros And Cons Collection Java
- Hashmap Custom Implementation Java
- Different Sorting Algorithms