How to return a list of keys from a Hash Map?
List<String> yourList = new ArrayList<>(map.keySet());
This is will do just fine.
Using map.keySet()
, you can get a set of keys. Then convert this set into List
by:
List<String> l = new ArrayList<String>(map.keySet());
And then use l.get(int)
method to access keys.
PS:- source- Most concise way to convert a Set<String> to a List<String>
map.keySet()
will return you all the keys. If you want the keys to be sorted, you might consider a TreeMap
Use the keySet()
method to return a set with all the keys of a Map
.
If you want to keep your Map ordered you can use a TreeMap
.