This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Convert HasMap to Arraylist ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert HasMap to Arraylist ??" Watch "Convert HasMap to Arraylist ??" New topic

Convert HasMap to Arraylist ??

Niara Bhonde

Joined: Jan 02, 2008
Posts: 10
Hi All,

Could anyone give me an example of how to convert a HashMap to an Arraylist?

I have a method which returns a Map of userID's and userNames.
I need to convert this Map to an ArrayList so I can use the size, and get methods on it.

Thanks in advance!

Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210


Can you elaborate more on what you are trying to do?
There are already size() and get() methods on Map.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

This question does not seem to be about the SCJP exam. I am moving this to the Java in General (Beginner) forum. Please continue there.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Check following methos


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38033
You don't need to convert the Map to get size() or get(). Maps and Lists are totally different. If you really want to iterate through the Map use the entrySet() method which gives a Set you can iterate through.

Other people have already told you this.
Javier Elizondo

Joined: Feb 08, 2003
Posts: 5
Try this:

Map aMap = new HashMap() ;
aList = new ArrayList(aMap.values ());
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38033
That is a very good suggestion, Javier; it only needs a type declaration which would be List<E>.
Please keep helping, but we usually try not to post on such an old topic; we have a badly-named FAQ about that.

Please keep posting, but watch the dates on the previous posts.

subject: Convert HasMap to Arraylist ??
Similar Threads
Convert from HashMap to ArrayList
Convert from one collection to anonther..?
ListIterator to ArrayList ?
Quickest way to convert a map to a list
how to store map value in string array