Computer Science Canada

Map -> Arraylist

Author:  Pockets [ Mon Nov 10, 2008 2:05 am ]
Post subject:  Map -> Arraylist

Is there a method out there to strip out the keys to a Map and return an ArrayList of all the elements? In the context here, the keys are unimportant and can be discarded.

Author:  DemonWasp [ Mon Nov 10, 2008 9:51 am ]
Post subject:  RE:Map -> Arraylist

Without speaking to any kind of efficiency concern, you could use:

Java:
map.entrySet().toArray


to get an array. Array -> ArrayList is trivial too.

Author:  OneOffDriveByPoster [ Mon Nov 10, 2008 10:11 am ]
Post subject:  Re: RE:Map -> Arraylist

DemonWasp @ Mon Nov 10, 2008 9:51 am wrote:
Java:
map.entrySet().toArray
The above won't get you just the values of course. Anyhow, why do you need the conversion? Do you have methods you need to call that need an ArrayList?

Author:  Pockets [ Mon Nov 10, 2008 4:35 pm ]
Post subject:  Re: Map -> Arraylist

Yeah, it's for a game of hearts. We're handed a trick as a map of <playerID strings, Card objects>, and I need to strip the card objects into a simple array for use in a card counting function. I may just use a manual iterator though.

Author:  OneOffDriveByPoster [ Mon Nov 10, 2008 4:56 pm ]
Post subject:  Re: Map -> Arraylist

Pockets @ Mon Nov 10, 2008 4:35 pm wrote:
I may just use a manual iterator though.
This is probably a cleanest thing to do. If you are not sure you will always have a Map, then use a Iterator from Map.values(). Not sure why you wanted an ArrayList specifically. Map.values() will give you a Collection view of the map's values. Your algorithm might not need to have something as specific as ArrayList.


: