Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need to store all keys of a hashMap in to an arrayList. this is my solution

807589Nov 18 2008 — edited Nov 18 2008
Hello,
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)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 16 2008
Added on Nov 18 2008
12 comments
2,018 views