Use java 8 streams to convert this:
List<Map.Entry<String, ?>> entryList = new List<>(//initialization);
List<String> stringList = entryList.stream().map(Entry::getKey).collect(Collectors.toList());
This makes a stream of the entries, then uses the map method to convert them to strings, then collects it to a list using Collectors.toList()
.
Alternatively, this method can be changed in a helper function if you need it more times like this:
public static <K> List<K> getKeys(List<Map.Entry<K,?>> entryList) {
return entryList.stream().map(Entry::getKey).collect(Collectors.toList());
}
public static <V> List<V> getValues(List<Map.Entry<?,V>> entryList) {
return entryList.stream().map(Entry::getValue).collect(Collectors.toList());
}
While the above code works, you can also get a List<K>
from a map by doing new ArrayList<>(map.keySet())
, with this having the advantage than you don't need to convert the entryset
to a list, before converted to a stream, and then back a list again.
entryList.stream().map(Map.Entry::getKey).collect(Collectors.toList())
Nicely made question, by the way. – RAnders00 Jan 13 '16 at 21:13