VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-convert-linkedhashmap-to-two-arrays-in-java/

⇱ How to Convert LinkedHashMap to Two Arrays in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Convert LinkedHashMap to Two Arrays in Java?

Last Updated : 23 Jul, 2025

LinkedHashMap is a pre-defined class in java like HashMap. The only difference between LinkedHashMap and HashMap is LinkedHashMap preserve insertion order while HashMap does not preserve insertion order. LinkedHashMap can convert into two Arrays in java where one array is for key in LinkedHashMap and another array for Values. 

Example :

Input : LinkedHashMap = [2=6, 3=4, 5=7, 4=6]
Output:
Array of keys = [2, 3, 5, 4]
Array of Values = [6, 4, 7, 6]

Input : LinkedHashMap = [1=a, 2=b, 3=c, 4=d]
Output:
Array of keys = [1, 2, 3, 4]
Array of Values = [a, b, c, d]

Algorithm :

  1. Take input in LinkedHashMap with keys and respective values.
  2. Create one array with data type the same as that of keys in LinkedHashMap.
  3. Create another array with data type the same as that of values in LinkedHashMap.
  4. Start LinkedHashMap traversal.
  5. Copy each key and value in two arrays.
  6. After completion of traversal, print both the arrays.

Below is the implementation of the above approach:


Output
Array of Key -> 2, 3, 5, 4, 6, 
Array of Values -> 6, 4, 7, 6, 8, 

Time Complexity: O(n)

Comment