• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert HasMap to Arraylist ??

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check following methos

1.HashMap.entrySet()
2.HashMap.keySet()
3.HashMap.Values()
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

Map aMap = new HashMap() ;
aList = new ArrayList(aMap.values ());
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic