Results 1 to 3 of 3
  1. #1
    maz09 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Unhappy Problem with HashMap

    Hi,

    with regards to accessing elements from a HashMap, I am aware of two ways...

    1. using a hashmap iterator.
    2. using a standard java for loop.

    However, i need to put a HashMap as a parameter of a recursive method, and i want to be able to access two things separately.

    1. The first element of the HashMap.
    2. Iterate through every element of the HashMap - EXCLUDING the first. (so they can be compared with the first for example)

    Please advise on how this could be done.

    Thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    A HashMap doesn't have a "first" element, or at least not a "hard" first element. HashMap does not guarantee in which order things will be returned. Maybe you want to use a LinkedHashMap, then things will be returned in the same order in which the keys are added (modifications will not change the order). The other way is to get the HashSet of keys of using the keys() and call toArray on that and pass both the array and the HashMap (as well as the "current index") and calling get on the HashMap using the key from the array.

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Who is creator of that HashMap and does it have to be HashMap at all?

    If it has to be please explain what are K,V of HashMap.

    This smells like List recursion to me...

Similar Threads

  1. Something like HashMap
    By BigBear in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 11:08 AM
  2. problem with removing element from HashMap
    By checho in forum New To Java
    Replies: 15
    Last Post: 01-10-2010, 01:10 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. Iterating through HashMap problem
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 11:28 PM
  5. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •