4

I am inserting four values with different keys in a HashMap. Code Snippet :

HashMap<Integer, String> choice = new HashMap<Integer, String>();

choice.put(1, "1917");
choice.put(2, "1791");
choice.put(3, "1902");
choice.put(4, "1997");

But when I am printing that map values,it returns a result something like :

{4=1997, 1=1917, 2=1791, 3=1902}

How can I get the map values in a sequential order the way I have put/inserted?

assylias
  • 321,522
  • 82
  • 660
  • 783
Rajhrita
  • 105
  • 1
  • 3
  • 11
  • 1
    If you want the Map sorted by key you can use TreeMap. This way you would get 1,2,3,4 as keys regardless of the order you added them. I use LinkedHashMap instead of HashMap as its easier to debug. e.g. when you add a key it will always be at the end. ;) – Peter Lawrey Jul 23 '12 at 13:05
  • possible duplicate of [java: HashMap iterator order](http://stackoverflow.com/questions/7029136/java-hashmap-iterator-order) – b4hand Feb 13 '15 at 01:44

3 Answers3

18

You can use a LinkedHashMap instead, which will keep the insertion 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).

You can modify your code like this:

Map<Integer, String> choice = new LinkedHashMap<Integer, String>();

//rest of the code is the same
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    The `LinkedHashMap` is a nice solution in that it provides O(1) performance on basic operations (add, remove, contains), contrary what you might expect from the term `Linked`. [Big 'O' notation and Java Constant time performance](http://objectissues.blogspot.com/2006/11/big-o-notation-and-java-constant-time.html). – Jeff Benshetler Jul 23 '12 at 13:00
  • One of the feasible solution. – Gaurav Koradiya May 11 '20 at 16:25
2

LinkedHashMap, which is a Hash table and linked list implementation of the Map interface can be used.

It maintains the same order in which the values are inserted using doubly linked list.

Reference : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html

Jeevi
  • 2,962
  • 6
  • 39
  • 60
0

[Edit]What the OP asks is not entirely clear to me. The OP could be asking, "How do I retrieve items from a map in insertion order?" If that is what the OP meant, then the following information is not a solution.

If the OP is asking, "How do I retrieve items from a Hash sorted by the key?" then the following is on-point. Getting the sorted keys of a hash/associative array is a common operation.


The answer can be found in how to sort Map values by key in Java:

List sortedKeys=new ArrayList(yourMap.keySet());
Collections.sort(sortedKeys);

If you care about the order in which the keys are stored and retrieved, consider using a TreeMap instead. Unless you have a very large number of elements (millions+), the performance difference is not likely to be noticeable.

Community
  • 1
  • 1
Jeff Benshetler
  • 640
  • 6
  • 13
  • 1
    Wrong advice. OP is interested in insertion order. – Marko Topolnik Jul 23 '12 at 12:57
  • This will sort based on the natural order of the keys (here ascending order of integers), not insertion order. – assylias Jul 23 '12 at 12:58
  • The commentators are correct and my advice is wrong. I'm kinda new here: what is the preferred action when one has posted a wrong answer? – Jeff Benshetler Jul 23 '12 at 13:04
  • @JeffBenshetler If you think it is useful for future users (for example, it is a common mistake), you can edit your answer and add a note explaining why it does not work. Otherwise, you can simply delete your answer. In this case, it seems your answer was based on a misunderstanding so you could probably simply delete it. – assylias Jul 23 '12 at 13:08
  • 1
    @JeffBenshetler Unless you think that the OP (original poster) meant a map sorted by its keys (the question is not 100% clear). – assylias Jul 23 '12 at 13:11