This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashmap " Watch "Hashmap " New topic
Author

Hashmap

akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
HashMap stores the contents in a key value pairs.So is it possible to store set of 'keys' in one arraylist, set of values in another arraylist and then send these arraylists to hashmap.
Gave a try to do the same. But did not get the concept of how the arraylist contents gets stored in HashMap.

Example:
Arraylist first has set of key - apple, orange, papaya
Arraylist second has set of values - red, yellow, green

so now the two arraylists have to be passed to hash map
HashMap<ArrayList, ArrayList> test = new HashMap<ArrayList, ArrayList>()

Can someone please help to get the concept of how the contents gets stored in arraylist and HashMap.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

akshitha Akki wrote:HashMap stores the contents in a key value pairs.So is it possible to store set of 'keys' in one arraylist, set of values in another arraylist and then send these arraylists to hashmap.
Gave a try to do the same. But did not get the concept of how the arraylist contents gets stored in HashMap.

Example:
Arraylist first has set of key - apple, orange, papaya
Arraylist second has set of values - red, yellow, green

so now the two arraylists have to be passed to hash map
HashMap<ArrayList, ArrayList> test = new HashMap<ArrayList, ArrayList>()

Can someone please help to get the concept of how the contents gets stored in arraylist and HashMap.


I don't think that the core library has anything that does it automatically -- but you can easily write code that loops through the arraylists to add the pairs to the hashmap.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
akshitha Akki
Greenhorn

Joined: Aug 03, 2012
Posts: 24
Yes I did try to do that, but i was unable to get the name from one arraylist, value from another arraylist and store them in a hashmap at the same time.Can I get a hint to get that concept so that I can do that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Really? You can't come up with any code which iterates through a list? Because that would be your first step, wouldn't it? Remember that your requirement is "Iterate through two ArrayLists and..." so iterating would be the first thing to work on.
 
wood burning stoves
 
subject: Hashmap
 
Similar Threads
SET function
Comparing Arraylist that is a value of Hashmap
HASH MAP & ARRAY LIST
In Struts,<logic:iterate> to iterate over values of hashmap which are arraylist of arraylists
how hashCode() is used in HashMap?