![]() |
VOOZH | about |
In Java, a Map is a collection that maps keys to values and a list is an ordered collection of objects and the List can contain duplicate values. There are some scenarios where we need to convert a Map into a List. In this article, we will explore how to convert the keys, values, or entries from a Map into a List.
The Map interface provides a method keySet() that returns a set of all the keys in the map. We can convert this set into a list using the ArrayList constructor.
Example: This example demonstrates how to convert the keys of a Map into a List in Java.
List of Keys: [Geek3, Geek2, Geek1]
The Map interface provides a values() method that returns collection of all the values in the map. We can convert this collection into a list by passing it to the ArrayList constructor.
Example: This example demonstrates how to convert the values of a Map into a List in Java.
List of values: [300, 200, 100]
From Java 8, we can use the Stream API to make the conversion process even more easy and functional. We can stream the map key or values and collect them into a list.
Example: This example demonstrates how to convert the keys and values of a Map into separate List object using Java Streams.
List of Keys using Streams: [Geek3, Geek2, Geek1] List of Values using Streams: [300, 200, 100]