This discussion is archived
3 Replies Latest reply: Apr 23, 2013 2:31 AM by Tolls RSS

Accessing map values via each keySet

1003073 Newbie
Currently Being Moderated
I am trying code a method where each key in a Map is iterated through keySet in a for-each loop but while each key is being accessed I need to also access its values where the values are of the type Set<Integer>. The Map is named bookIndex and I have tried using bookIndex.values() but this accesses all the values of the Map and not just the values of the key being presently accessed. Ive tried using a List to do this but was advised that this was not needed in another thread (Accessing Map values through a List). Ive tried creating a Set to contain these values but bookIndex.values() does not do it.
  • 1. Re: Accessing map values via each keySet
    baftos Expert
    Currently Being Moderated
    1000070 wrote:
    I am trying code a method where each key in a Map is iterated through keySet in a for-each loop but while each key is being accessed I need to also access its values where the values are of the type Set<Integer>. The Map is named bookIndex and I have tried using bookIndex.values() but this accesses all the values of the Map and not just the values of the key being presently accessed. Ive tried using a List to do this but was advised that this was not needed in another thread (Accessing Map values through a List). Ive tried creating a Set to contain these values but bookIndex.values() does not do it.
    So in each iteration you have a key myCurrentKey. Why bookIndex.get(myCurrentKey) is not good?
  • 2. Re: Accessing map values via each keySet
    1003073 Newbie
    Currently Being Moderated
    Thankyou It works
  • 3. Re: Accessing map values via each keySet
    Tolls Journeyer
    Currently Being Moderated
    1000070 wrote:
    I am trying code a method where each key in a Map is iterated through keySet in a for-each loop but while each key is being accessed I need to also access its values where the values are of the type Set<Integer>.
    entrySet.
    http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()
    That gives you the keys and values in one go.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points