Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

HashMap - Urgent

Hi,

I am learning maps and i ran the program below which gives me :

 {E542=Brendan, E123=Charles, E174=Tobin}

I find this strange as at first i thought it was printing based on the asending order. But later, when I changed E174 to to Aaa. it still prints:
 {E542=Brendan, E123=Charles, E174=Aaa}

Pls clarify. how does the hash map prints number ?


import java.util.*;
public class MapTest1 {
   public static void main(String[] args) {
      // Hasp map mapping employee number to accounts
      Map accountsMap = new HashMap();
      accountsMap.put("E123","Charles");
      accountsMap.put("E174","Matt");
      accountsMap.put("E542","Brendan");
      accountsMap.put("E174","Tobin");
      System.out.println(accountsMap);
   }
}
0
zizi21
Asked:
zizi21
  • 7
  • 3
  • 3
2 Solutions
 
CEHJCommented:
HashMap is unordered. Use TreeMap for sorted keys, LinkedHashMap for order of addition
0
 
zizi21Author Commented:
but how do you know, what is printed out first.

It could have been : {E123=Charles, E542=Brendan, E174=Tobin}

as i entered charles first, then brendan lastly tobin.
0
 
CEHJCommented:
In case it's not clear, any ordering is based on keys, not values
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>but how do you know, what is printed out first.

You don't. It's unordered
0
 
CEHJCommented:
If you need to know, use one of the approaches i mentioned at first
0
 
zizi21Author Commented:
Hi,

I looked at the tut above and got this:

import java.util.HashMap;
import java.util.Map;

public class MainClass {
  public static void main(String[] a) {

    Map map = new HashMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    System.out.println(map);
  }
}
   
{key1=value1, key3=value3, key2=value2}

Similarly, key 1 is printed than key3 and lastly key2.
>>
>>CEHJ:
>>In case it's not clear, any ordering is based on keys, not values

This part i am confused. Since ordering is done based on keys, it should have key 1 then, key 2 and lastly key3.

Sorry, I am a bit too slow to understand..
0
 
objectsCommented:
Order is *not* done on key for a HashMap. It is an unordered collection, ie. the order can be whatever it wants to be.
Try a TreeMap, it does order on key.
0
 
CEHJCommented:
>>This part i am confused. Since ordering is done based on keys, it should have key 1 then, key 2 and lastly key3.

Only in a TreeMap
0
 
objectsCommented:
>     Map map = new HashMap();

change to:

    Map map = new TreeMap();
0
 
CEHJCommented:
Map map = new TreeMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
0
 
zizi21Author Commented:
Got it . thank you very much. :-)
0
 
CEHJCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now