Forums Register Login

Make HashMap elements fit into an ArrayList

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.



_M_
+Pie Number of slices to send: Send
Hi, I would like to store them both in the array. Your post should do the job. But I'd still like to know how... Thanks!
+Pie Number of slices to send: Send
Use the inner class Map.EntrySet like this



Regards,
Edwin Dalorzo
+Pie Number of slices to send: Send
Thanks... But I think I got a little lost in these lines...


+Pie Number of slices to send: Send
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.

Regards,
Edwin Dalorzo.
[ January 16, 2006: Message edited by: Edwin Dalorzo ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Timothy, comrade, Set is a Collection

Hashset extends:

Object<-AbstractCollection<-AbstractSet<-HashSet

HastSet implements Collection and Set interfaces.

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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
My pleasure, pal.

Good for you, your code looks pretty well.

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.

See you around!
+Pie Number of slices to send: Send
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.

Good luck!

Layne
+Pie Number of slices to send: Send
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:


Garrett
[ January 18, 2006: Message edited by: Garrett Rowe ]
+Pie Number of slices to send: Send
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...
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
https://gardener-gift.com


reply
reply
This thread has been viewed 31817 times.
Similar Threads
Simple ArrayList conundrum?
Blocks synchronized, but without wait() or notifyAll() methods.
Locking Schemes: Tactical View 01
Single table / Simple Locking - WeakHashMap vs WeakReferences
LinkedList vs ArrayList
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:58:52.