File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Make HashMap elements fit into an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Make HashMap elements fit into an ArrayList" Watch "Make HashMap elements fit into an ArrayList" New topic
Author

Make HashMap elements fit into an ArrayList

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
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_


Mike Noel
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Use the inner class Map.EntrySet like this



Regards,
Edwin Dalorzo
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Thanks... But I think I got a little lost in these lines...


Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 ]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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 ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 ]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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...


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make HashMap elements fit into an ArrayList