• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap<String, ArrayList>

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic