my dog learned polymorphism*
The moose likes Java in General and the fly likes HashMap<String, ArrayList> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap<String, ArrayList>" Watch "HashMap<String, ArrayList>" New topic
Author

HashMap<String, ArrayList>

Eugene Wright
Greenhorn

Joined: Mar 22, 2011
Posts: 10
I've created a Hashmap like the following HashMap<String, ArrayList> myMap = new HashMap<String, ArrayList>();

I've added my keys and values, and I understand how to do a straight iteration through my HashMap, but how do I access only the "5th" item in the ArrayList (just as an example).

So I know the 5th item in the ArrayList for each of the hashmap values will be a phone number, I want to iterate through the HashMap and only grab that 5th value.

Thanks
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Eugene: As you described, you first get the keySet() of your Map and then
using for(each), iterate through this Set, get() each List and pick up its 5th element.

Jim ...


BEE MBA PMP SCJP-6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
. . . which will of course throw a NullPointerException if the name "Eugene" doesn't appear as a "key".

But you have a serious design problem. Why on earth is the phone number the 5th item in the List? Why haven't you created a class to encapsulate all those values?
 
 
subject: HashMap<String, ArrayList>
 
Similar Threads
Adding key/value pairs from a HashMap in to an ArrayList
Struts2: Retern all the values inside the list in my Action
HashMapping
Getting a single name from an arraylist...
Adding values to a list already against already existing key in HashMap