I need to convert a HashMap<String, Object>
to an array; could anyone show me how it's done?
|
|||||
|
Edit Should be noted that the ordering of both arrays may not be the same, See oxbow_lakes answer for a better approach for iteration when the pair key/values are needed. |
|||||||||||||||||||
|
If you want the keys and values, you can always do this via the
From each entry you can (of course) get both the key and value via the |
|||
|
If you have
will return an Object[]. If instead you want an array of the type SomeObject, you could use:
|
|||||||
|
To guarantee the correct order for each array of Keys and Values, use this (the other answers use individual
|
||||
|
An alternative to CrackerJacks suggestion, if you want the HashMap to maintain order you could consider using a LinkedHashMap instead. As far as im aware it's functionality is identical to a HashMap but it is FIFO so it maintains the order in which items were added. |
|||||
|
|
||||
|
You may try this too.
Here I am using String as return type. You may change it to required return type by you. |
|||
|