• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap<String, ArrayList>

 
Eugene Wright
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49367
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic