Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: hash map iteration

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default hash map iteration

    i have a problem in iterating hashmap..
    it gives me values in reverse order...why so..please help me...
    here is my code..
    cart="1.1,2.2,3.3,1.1";// cart variable

    String[] cart_items=cart.split(",");// spliting cart by ' , '

    HashMap<String,Integer> hashmap=new HashMap(); // create a new hashmap
    for(String i:cart_items)// iterate through cart_items
    {

    if(hashmap.containsKey(i)) //if hashmap contains key increment value by 1
    {
    f+=1;
    hashmap.put(i,f);
    }
    else
    {
    f=1;
    hashmap.put(i, f); // if hashmap doesnt contain key put its values as 1
    }
    }
    //now i want to iterate through this hashmap....

    for(Object j:hashmap.keySet() )
    {
    out.println("key=" + i + " and value= " + hahsmap.get(i) );


    }


    /////////////// now this should give me output as..
    key=1.1 and value=2
    key=2.2 and value=1
    key=3.3 and value=1

    ////////////but it gives me output as....

    key=3.3 and value=1
    key=2.2 and value=1
    key=1.1 and value=2

    /////////////

    please help me why this is giving me such output..........................


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,441
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: hash map iteration

    You don't control the order you get the keys in a HashMap. Maps aren't really designed for sequential iteration- instead, they're designed to make "give me this thing that might be in the middle somehwere" fast. You might look at a TreeMap instead.

    Alternatively, you could look into sorting the key set, or you could just use the array of keys to get them in the order you want them.

    Although, this does bring up the question- why are you using a Map in the first place?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: hash map iteration

    To add to Kevin's advice, if you want order of iteration to remain the same as order of addition for a Map, use a LinkedHashMap.

  4. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (September 10th, 2011)

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. [SOLVED] MD5 hash and MimeMessage problem
    By ArgyrisV in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 19th, 2011, 03:28 PM
  3. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM
  4. Converting a recursion to iteration
    By javaplus in forum Algorithms & Recursion
    Replies: 7
    Last Post: March 3rd, 2010, 04:38 PM
  5. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM

Tags for this Thread