This example shows how to convert LinkedHashMap to two arrays in Java such that the one array contains all the keys of the LinkedHashMap and another all values.
How to convert LinkedHashMap to two arrays in Java?
The LinkedHashMap class maps keys to values while the array in Java is a collection of the same type of elements. However, you can put all the keys of HashMap in one array and all the values in an another array.
To get all the keys of the LinkedHashMap, you can use the keySet
method.
1 |
public Set<K> keySet() |
This method returns a Set view of all the keys contained in the LinkedHashMap object. Once you get all the keys in a Set, use the toArray
method of the Arrays class to convert it to an array.
Similarly, you can use the values
method of the LinkedHashMap class to get all the values contained in the map.
1 |
public Collection<V> values() |
This method returns a Collection view of all the values stored in the LinkedHashMap object. Once you get all the values in a Collection, use the toArray
method of the Arrays class to convert Collection to an array.
The below given example shows how to convert the key set and value collection to two arrays.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.Collection; import java.util.LinkedHashMap; import java.util.Set; public class LinkedHashMapToTwoArraysExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); /* * Get all the keys of LinkedHashMap */ Set<Integer> setKeys = lhmap.keySet(); /* * To convert key set to an array, use the * toArray method and pass an array of the same size */ Integer[] arrayKeys = setKeys.toArray( new Integer[setKeys.size()] ); /* * Get all the values of LinkedHashMap */ Collection<String> values = lhmap.values(); /* * To convert values collection to an array, use the * toArray method and pass an array of the same size */ String[] arrayValues = values.toArray( new String[values.size()] ); //iterate key array System.out.println("Key Array: "); for(Integer key : arrayKeys){ System.out.println( key ); } //iterate value array System.out.println("\nValue array: "); for(String value : arrayValues){ System.out.println( value ); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Key Array: 1 2 3 4 5 Value array: One Two Three Four Five |
Important Note:
Always make sure to pass the array of the same size as that of Set or a Collection to the toArray
method. The toArray
method returns the same array if it is large enough to contain all the elements of the Set or a Collection. If not, it allocates a new array, fills it with elements and returns it.
Since an array allocation is a costly operation in terms of performance, it is a good practice to pass the array of the required length to the toArray
method.
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap