Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are the characteristic differences of some important <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html" rel="noreferrer"><code>Map</code></a> implementations:</p> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/LinkedHashMap.html" rel="noreferrer"><code>LinkedHashMap</code></a>: "with predictable iteration order [...] which is normally the order in which keys were inserted into the map (<em>insertion-order</em>)."</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/HashMap.html" rel="noreferrer"><code>HashMap</code></a>: "makes no guarantees as to the order of the map"</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html" rel="noreferrer"><code>TreeMap</code></a>: "is sorted according to the natural ordering of its keys, or by a <code>Comparator</code>" <ul> <li>i.e. it's a <a href="http://java.sun.com/javase/6/docs/api/java/util/SortedMap.html" rel="noreferrer"><code>SortedMap</code></a></li> </ul></li> </ul> <p>So it looks like <code>LinkedHashMap</code> is what you need in this case.</p> <p>Here's a snippet to illustrate the differences; it also shows a common way to iterate over all entries of a <code>Map</code>, and how using an interface to refer to objects allow great flexibility of choice of implementation.</p> <pre><code>import java.util.*; public class MapExample { public static void main(String[] args) { populateThenDump(new HashMap&lt;String,Integer&gt;()); populateThenDump(new TreeMap&lt;String,Integer&gt;()); populateThenDump(new LinkedHashMap&lt;String,Integer&gt;()); } static void populateThenDump(Map&lt;String,Integer&gt; map) { System.out.println(map.getClass().getName()); map.put("Zero", 0); map.put("One", 1); map.put("Two", 2); map.put("Three", 3); map.put("Four", 4); for (Map.Entry&lt;String,Integer&gt; entry : map.entrySet()) { System.out.println(entry.getKey() + " =&gt; " + entry.getValue()); } } } </code></pre> <p>The output of the above snippet is (<a href="http://ideone.com/ADouC" rel="noreferrer">as seen on ideone.com</a>):</p> <pre><code>java.util.HashMap // unordered, results may vary Three =&gt; 3 Zero =&gt; 0 One =&gt; 1 Four =&gt; 4 Two =&gt; 2 java.util.TreeMap // ordered by String keys lexicographically Four =&gt; 4 One =&gt; 1 Three =&gt; 3 Two =&gt; 2 Zero =&gt; 0 java.util.LinkedHashMap // insertion order Zero =&gt; 0 One =&gt; 1 Two =&gt; 2 Three =&gt; 3 Four =&gt; 4 </code></pre> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/46898/iterate-over-map">Iterate Over Map</a></li> <li><a href="https://stackoverflow.com/questions/1884889/iterating-over-and-removing-from-a-map">iterating over and removing from a map</a> <ul> <li>If you want to modify the map while iterating, you'd need to use its <a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html" rel="noreferrer"><code>Iterator</code></a>.</li> </ul></li> </ul> <h3>Similar questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/1419708/how-to-keep-the-order-of-elements-in-hashtable">How to keep the order of elements in hashtable</a></li> <li><a href="https://stackoverflow.com/questions/1190083/does-entryset-in-a-linkedhashmap-also-guarantee-order">Does entrySet() in a LinkedHashMap also guarantee order?</a></li> <li><a href="https://stackoverflow.com/questions/683518/java-class-that-implements-map-and-keeps-insertion-order">Java Class that implements Map and keeps insertion order?</a></li> <li><a href="https://stackoverflow.com/questions/1212198/ordered-list-map-implementation-in-java">Ordered List Map implementation in Java</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload