• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert HasMap to Arraylist ??

 
Niara Bhonde
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check following methos

1.HashMap.entrySet()
2.HashMap.keySet()
3.HashMap.Values()
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

Map aMap = new HashMap() ;
aList = new ArrayList(aMap.values ());
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic