File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 15080

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 8 API documentation
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: 46320
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: 46320
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.

I agree. Here's the link:
subject: Convert HasMap to Arraylist ??
It's not a secret anymore!