Need to store all keys of a hashMap in to an arrayList. this is my solution
807589Nov 18 2008 — edited Nov 18 2008Hello,
I Need to store all keys of a hashMap in to an arrayList. this is my solution:
private HashMap<String, V> x;
.
.
.
.
ArrayList<V> xList = new ArrayList <V>(x.values());
ArrayList<String> xKeyList = null;
for(V currV: xKeyList){
xKeyList.add(currV.getKey());
}
so because I couldn't figure out a way to use:
"Set<K> keySet()
Returns a set view of the keys contained in this map."
in order to create a ArrayList I decided to get an array of the Values of the hashMap and then iterate through those values and get the keys (names in my case, yes I use names as keys of the hashmap and I can get the name of my V individually)
what do you think? was this a good approach or I could be using hashMap.keySet() in a better way?
Thanks for all your input!
(I'm aware that my solution would be very heavy toll on performance for very large maps~ that's why i'm here :D)