How can I convert a Map<K, V>
into listsList<K> keys
, List<V> values
, such that the order in keys
and values
match? I can only find Set<K> Map#keySet()
and Collection<V> Map#values()
which I could convert to lists using:
List<String> keys = new ArrayList<String>(map.keySet());
List<String> values = new ArrayList<String>(map.values());
but I worry that the order will be random.
Is it correct that I have to manually convert them or is there a shortcut somewhere?
Update: Owing to the answers I was able to find additional, useful information which I like to share with you, random google user: How to efficiently iterate over each Entry in a Map?