View Full Version : How to convert HashMap to List
I want to convert an HashMap<int, Class> to List<Class>. Kindly help me how to do this.
10 Jul 2009, 5:34 AM
I'm assuming that you want to get the collection of values from the HashMap.
Let's say your HashMap<Integer, Class> is called hashMap.
Most implementations of List have a constructor that takes in a collection. You can get a collection of the values from the values() method. To answer your question exactly using ArrayList:
List<Class> listOfClasses = new ArrayList<Class>(hashMap.values());
In the future you may want to ask basic java questions in a forum dedicated to java. You could also just look at the Java API documentation and figure it out yourself :-)
http://java.sun.com/javase/6/docs/api/ if you use Java 1.6
10 Jul 2009, 5:39 AM
By the way, why do you have a HashMap of int to Class? Wouldn't it be more efficient to use an ArrayList<Class> instead where the index acts like the key?
Thank you for your help. But i'm facing problem in retrieving the values from list, i'm not able to cast it to the Class.
10 Jul 2009, 5:55 AM
I'm not sure what you mean.
I would ask you to post some sample code, but I might get yelled at by the moderator for continuing this non-GXT related conversation :s. Try asking on a Java forum (like Sun's: http://forums.sun.com/index.jspa) where it may be more appropriate.
but I might get yelled at by the moderator for continuing this non-GXT related conversation
Why should you? There is no problem with this.
10 Jul 2009, 6:15 AM
Because this is a forum for GXT help, not Java help?
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.