Hi, I want to put every element of a HashMap to an ArrayList. Is this possible? Let's say my HashMap(a single object) has 10 elements in it... I would like to make that fit into an ArrayList of 10 elements as well. Thanks!
A HashMap stores key=value pairs. An ArrayList just stores single values. Do you want to store all of the keys, values, or both in the array? If you want the keys you can use the keySet() method to return a Set of keys. If you want the values you can use the values() method to return a Collection of values. Either the Set or the Collection can be used to create the ArrayList.
See javadoc about the java.util.Map class. There you will see that this class has an static inner class named Entry which is a key-value pair representation of the contents of the Map.
Every element in the map is actually a Map.Entry instance, this Entry class has three main methods: getKey(), getValue(), and setValue(Object o).
You can get access to the Map.Entry items by means of invoking the method entrySet() on a Map instance.
Thanks! Ok I have looked into the docs. I can feel the information going through my brain now... I just need a little more explanation I guess...
I'm a little confused with this line... In the java documentation, it said that the ArrayList constructor accepts a Collection argument. However, Map.entrySet returns a Set object. How is this possible? [ January 16, 2006: Message edited by: Timothy Sam ]
Maybe you are a bit confused with Map, because Map is not a Collection.
But the method myMap.entrySet() does not return a Map, but a Set, and Set is indeed a Collection.
Hence, I am just using an inheritance principle that I hope you understand, because I cannot explain it here. I just can tell you this:
If...
Animal<-Mammal<-Tiger Animal<-Mammal<-Dog
Then all dogs are Mammals and all Tigers are Mammals. So If I had method that accepts a Mammal as parameter I could pass wheter a Tiger or a Dog, because both a Mammals. It is the same principle with Collections.
All Sets and Lists are actually a Collection, so wherever you see a Collection you can substitute it for a Set or List.
However if you still do not know this basic principles I suggest you to visit The Java Tutorial and get yourself started.
Regards, Edwin Dalorzo.
Good luck, pal. [ January 16, 2006: Message edited by: Edwin Dalorzo ]
Thanks! Sorry... Of course, I do understand the concept of inheritance. I guess I just overlooked at the docs and I really didn't know the underlying implementation of those classes (such as which inherits or implements who).
I had to use this idea in my bean, I came into something like this...
where departmentEntries is an instace of the class ArrayList. I plan on accessing each item on another class(a servlet if you ask). Thanks, I was very enlightened. And oh...
Timothy, comrade, Set is a Collection
it's very flaterring to be called like this in javaranch. Don't worry, I won't be bugging you much.
You can bug all that you want, we all are here for that... and hey, one final piece of advise, if you want to improve your skills as a programmer know the API, and pretty soon you will be Java Guru.
To get a better handle on how to use the Collections framework, you should check out the Collections Trail from Sun's Java Tutorial. I think this tutorial will help solidify the answers given to your original question here. It will also help you understand the inheritance hierarchy that relates all the classes and interface involved here.
Also, since Java 5, there are type-safe parameterized HashMaps, ArrayList, List, Set etc., which eliminate the need for casting, and the for-each loop which eliminates the need for an Iterator in this case. Therefore the same code could be written as:
One last thing you should be aware of is that your list contains references to the entries in your map. If you change entry values via the list, the changes will show up in the map. This may be what you want, or you may not be planning to alter the entries, or this may be an unpleasant surprise, all depending on your design. Demo:To fix this -- heck, you'll know what to do once you read that tutorial...
Post by:autobot
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, that will fit in a stocking