Convert Map to an array in Java
This post will discuss how to convert map to an array in Java.
1. Convert Map<K,V> to array of Map.Entry<K,V>
We can easily get an object array of Map.Entry<K,V> using Map.entrySet() with Set.toArray(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.HashMap; import java.util.Map; class Main { // Program to convert map to an array in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); Object[] objectArray = hashMap.entrySet().toArray(); System.out.println(Arrays.toString(objectArray)); } } |
Output:
[RED=#FF0000, BLUE=#0000FF]
2. Convert map to array of keys
We can get an array of keys of the map using Map.keySet() with Set.toArray(T[] a).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.HashMap; import java.util.Map; class Main { // Program to convert map keys into an array in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); // get the keys of the `HashMap` returned as an array String[] key = hashMap.keySet().toArray(new String[0]); System.out.println(Arrays.toString(key)); } } |
Output:
[RED, BLUE]
3. Convert map to array of values
We can get an array of values of the map using Map.values() with Collection.toArray(T[] a).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.HashMap; import java.util.Map; class Main { // Program to convert map values into an array in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); // get the values of the `HashMap` returned as an array String[] values = hashMap.values().toArray(new String[0]); System.out.println(Arrays.toString(values)); } } |
Output:
[#FF0000, #0000FF]
4. Convert map to array of key-value pairs
We have seen that we can get an array of keys and values of the map using Map.keySet() and Map.values(), respectively. We can easily construct an array of key-value pairs from key[] and value[].
We have used LinkedHashMap in the following program that maintains insertion-order iteration, which is the order in which keys were inserted into the map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.LinkedHashMap; import java.util.Map; class Main { // Program to convert `LinkedHashMap` to an array in Java public static void main(String[] args) { Map<String, String> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put("1", "one"); linkedHashMap.put("2", "two"); linkedHashMap.put("12", "twelve"); linkedHashMap.put("11", "eleven"); // get an array of keys of the `HashMap` String[] key = linkedHashMap.keySet().toArray(new String[0]); // get an array of values of the `HashMap` String[] value = linkedHashMap.values().toArray(new String[0]); for (int i = 0; i < linkedHashMap.size(); i++) { System.out.println( "{" + key[i] + "=" + value[i] + "}" ); } } } |
Output:
{1=one}
{2=two}
{12=twelve}
{11=eleven}
This seems pretty straightforward, but if HashMap or TreeMap is used, the ordering of both arrays might change, i.e., for any index i, there is no guarantee that key[i] will represent the original key for value[i]. To be on the safer side, we can construct key[] and value[] as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.HashMap; import java.util.Map; class Main { // Program to convert map to an array in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("1", "one"); hashMap.put("2", "two"); hashMap.put("12", "twelve"); hashMap.put("11", "eleven"); // array to store keys of the `HashMap` String[] key = new String[hashMap.size()]; // array to store values of the `HashMap` String[] value = new String[hashMap.size()]; int i = 0; for (Map.Entry<String, String> entry: hashMap.entrySet()) { key[i] = entry.getKey(); value[i++] = entry.getValue(); } for (i = 0; i < hashMap.size(); i++) { System.out.println( "{" + key[i] + "=" + value[i] + "}" ); } } } |
Output:
{11=eleven}
{1=one}
{12=twelve}
{2=two}
That’s all about converting Map to an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)