7

Here is my code to store the data into HashMap and display the data using iterator

public static void main(String args[]) {
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("aaa", "111");
    hm.put("bbb", "222");
    hm.put("ccc", "333");
    hm.put("ddd", "444");
    hm.put("eee", "555");
    hm.put("fff", "666");

    Iterator iterator = hm.keySet().iterator();

    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        String val = hm.get(key);

        System.out.println(key + " " + val);
    }
}

But it is not displaying in the order in which I stored. Could someone please tell me where am I going wrong? How can I get the elements in the order?

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
Santhosh
  • 4,956
  • 12
  • 62
  • 90

4 Answers4

27

A HashMap has no guaranteed order:

This class makes no guarantees as to the order of the map;

Use a LinkedHashMap.

Hash table and linked list implementation of the Map interface, with predictable iteration order.

Jacob
  • 41,721
  • 6
  • 79
  • 81
8

You need to use a LinkedHashMap because it maintains ordering of its entries, unlike HashMap.

From the javadocs:

... implementation of the Map interface with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

dogbane
  • 266,786
  • 75
  • 396
  • 414
3

HashMap does not keep order in which we put data into it.So You may follow LinkedHashMap instead.It keeps the order in which we put data.LinkedHashMap can be used same as HashMap.

Map<key,value> map=new LinkedHashMap<key,value>();
map.put("key","value");
map.put("key","value");
map.put("key","value");

//similarly you can use iterator to access data too.It will display dfata in order in which you added to it.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • duplicate ??? i never copy once answer. What i know i posted. That doesnot mean what i know nobody knows? Think before giving negative mark. – Android Killer Aug 09 '11 at 12:43
  • Look at the answers provided by others. They already explained that `HashMap` does not keep order, and suggested using `LinkedHashMap` as an alternative. Your answer provides nothing more than what has already been provided. It actually provides less, since you don't even link to the javadocs. I did give the down-vote some thought, and I even made sure I didn't do it anonymously - you're welcome. – mre Aug 09 '11 at 12:46
  • Every question has one similar answer and if there are more that one answer then all indicate to similar answer.That doesnot mean we copied.In SO every question get more than one answer generally and all answers have same meaning in any how.Why you only look at my answer.My answer has more description than others. – Android Killer Aug 09 '11 at 12:49
  • Your answer is not more descriptive, it just has more words. Everything else you said is just not true. If a question has already been sufficiently answered, most people will up-vote and refrain from providing their own answer. If they have something to add, they'll usually add their tidbit as a comment. – mre Aug 09 '11 at 12:53
  • If you do not need a answer then that does not mean that it is not good answer.It may help others. – Android Killer Aug 09 '11 at 12:58
  • 2
    @mre His answer came 3 minutes after the others. It probably took him > 3 mins to write it. – Qwerky Aug 09 '11 at 13:10
  • @Qwerky, You're both missing the point now. Whatever, done arguing with people. :D – mre Aug 09 '11 at 13:15
0

The reason is HashMap and HashSet doesn't guarantee the order of the stored values. Position of the elements will depends on the size of the internal table and hashCode of the key.

If you want to load your data in some order you need to sort keys/or values. For example you can put collections of the entries (Map.entrySet()) to the list and sort by any criteria you want. Or you can use SortedMap (TreeMap for example) to store your objects.