aspose file tools*
The moose likes Beginning Java and the fly likes Adding key/value pairs from a HashMap in to an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding key/value pairs from a HashMap in to an ArrayList" Watch "Adding key/value pairs from a HashMap in to an ArrayList" New topic
Author

Adding key/value pairs from a HashMap in to an ArrayList

bob fissle
Greenhorn

Joined: Feb 02, 2012
Posts: 6
I'm trying to create a HashMap, loop through that HashMap, and finally, I'm trying create an ArrayList of key/value pairs -- from the HashMap -- that contain within its value set a parameter value. I realize that might be a little hard to understand, so I've provided my code to make sense of what I'm trying to accomplish.



For the desired output we can assume that the productAttribute parameter value is "Computer".

Current output of the addProduct ArrayList:


Desired output of the addProduct ArrayList:


I can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18118
    
    8

So you have a map which maps a String to a list of Strings. And if I understand your example right, you want to mutate that into a list of lists of Strings, where each of the inner lists comprises a key of the map followed by the list it's mapped to?

Well, looking at the lists of Strings you have in your example, I don't understand why they are lists of Strings. To me they ought to be some kind of objects which are designed specifically for these products, because obviously the first entry in the list is some kind of a category, the second entry is a manufacturer, and so on. Just putting those things in a list is a misuse of lists.

And likewise I don't see the point of making a slightly different list which just has the product code on the front. Perhaps it should be part of the product object which I was just suggesting. Making a list of products would make a lot of sense once you've done that, which is basically your non-OO requirement anyway.
bob fissle
Greenhorn

Joined: Feb 02, 2012
Posts: 6
Thanks for the reply Paul. Yes, your understanding of what I'm trying to do is correct. Is there any chance you could provide some pseudo code on a better way of accomplishing the task at hand?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
This might be the beginnings of such a class. For the string types, other custom-made classes may be more appropriate; that depends on the design of your app and your requirements.
 
wood burning stoves
 
subject: Adding key/value pairs from a HashMap in to an ArrayList
 
Similar Threads
Apple iPad2 VS Asus PX-MU17-BU
Java + "for" loop
Read file from directory, update contents of the each file
New class not working
is rectangle?