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
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap<String, ArrayList>" Watch "HashMap<String, ArrayList>" New topic

HashMap<String, ArrayList>

Eugene Wright

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.

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 ...

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
. . . 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?
I agree. Here's the link:
subject: HashMap<String, ArrayList>
It's not a secret anymore!