Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make HashMap elements fit into an ArrayList

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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_
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the inner class Map.EntrySet like this



Regards,
Edwin Dalorzo
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... But I think I got a little lost in these lines...


 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic