How to Convert all LinkedHashMap Key-Value pairs to List in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order.
We have to convert all LinkedHashMap key-values pairs to list, so we have a LinkedHashMap object that contains some pairs of key-values and we have to convert it into list using keySet() and values() method.
keySet(): This method is used to get the keys of its called LinkedHashMap object.
values(): This method is used to get the values of its called LinkedHashMap object.
- Create a LinkedHashMap that contain some keys and values pair.
- Create a List1 that contain the keys of LinkedHashMap object.
- Create a List2 that contain the values of LinkedHashMap object.
List 1 - [1, 2, 3, 4, 5] List 2 - [One, Two, Three, Four, Five]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.