aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert HasMap to Arraylist ??" Watch "Convert HasMap to Arraylist ??" New topic
Author

Convert HasMap to Arraylist ??

Niara Bhonde
Greenhorn

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!

Niara.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Niara,

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

-DJ
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

1.HashMap.entrySet()
2.HashMap.keySet()
3.HashMap.Values()


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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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
Greenhorn

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

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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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.

CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert HasMap to Arraylist ??