I am getting all the keys in a map into a HashSet.
I want to be able to transfer this into an ArrayList.
I am getting all the keys in a map into a HashSet.
I want to be able to transfer this into an ArrayList.
Did you mean this :
Map<K, V> map = new HashMap<>();
List<K> list = new ArrayList<>(map.keySet());
It appears you are already aware that Set<KeyType> set = map.keySet()
returns a Set of the keys:
to put these into an ArrayList<KeyType>
you can simply do new ArrayList(set);
Why not use the addAll method?
Set<KeyType> keys = map.keySet();
List<KeyType> res = new ArrayList<>();
res.addAll(keys);
No?
Map<String,String> mymap = new HashMap<>();
// add data to the map
String[] array = mymap.keySet().toArray(new String[0]);
Arrays.asList(map.keySet().toArray());
Would be a good place to start