Convert Map to List in Java



This code example shows how to convert a Map-object to a List-object.

A Map consist of key and value pairs but every element in a List contains only one value. So either we have to convert every key item to a List item, every value item to a List item or a combination of key- and value items to a List item.

In the examples below a Map is created with months of a year. The key values are Integers which represents the number of each month, and the corresponding values are Strings with each month’s name.

Convert keys of a Map to a List

The output from the above code is:

Convert values of a Map to a List

This code example shows how to convert the values of a Map to a List:

The output from the above code is:

Convert keys and values of a Map to a List

This code example combines the key and value of a Map and store them in a List. It’s slightly more work here since we need to iterate through the keys of the map and fetch the corresponding value and then add them both to the list.

The output from this last example is:







Search for more Java info on this site here:
Custom Search


Please type any questions here.