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.
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.
Similarly, extract the values of a HashMap and construct an ArrayList out of it as below:
Now let us constuct a single ArrayList having both the keys and values of a HashMap in a single shot.
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.
Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.
©2017, DevGlan. All Rights Reserved.
Contact us: email@example.com