21

The following code gives me the output in the same order of insertion. I read the javadoc and they did not even talk about the insertion order. Can someone help me to get the right information.

import java.util.*;

public class hash {

public static void main(String[] args) {

    String str[] = { "japan",
            "usa",
            "japan",
            "russia",
            "usa",
            "japan",
            "japan",
            "australia"};
    int len = 8;
    Hashtable ht = new Hashtable();
    int i = 0;
    while (i < len) {

        String c = str[i];
        System.out.println("c :" + c);
        Integer intg = (Integer) ht.get(c);

        if (intg == null)
            ht.put(c, new Integer(1));
        else
            ht.put(c, new Integer(intg.intValue() + 1));

        i++;
    }

    Enumeration k = ht.keys();

    while (k.hasMoreElements()) {
        String key = (String) k.nextElement();
        System.out.println(key + " > " + ht.get(key));
    }
}
}
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
wayfare
  • 1,802
  • 5
  • 20
  • 37

5 Answers5

41

No, it does not. To preserve insertion order, instead use java.util.LinkedHashMap (javadoc).

Also, HashMap is now preferred over Hashtable, because Hashtable has unnecessary concurrency overhead. (See Differences between HashMap and Hashtable?.)

Community
  • 1
  • 1
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
9

no, it does not. it only knows the "hash" order. if you reorder the strings, you will find they still appear in the same order from the hashtable.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • 1
    *"if you reorder the strings, you will find they still appear in the same order from the hashtable"* - not always true. If there are collisions, then the hashtable order will **depend on** the key insertion order. So an attempt to reorder could change hashtable order, though not in the way that you want. – Stephen C Jun 20 '11 at 00:52
  • correct. I meant only to say that a trivial reordering of these particular strings yielded the same output. – jcomeau_ictx Jun 20 '11 at 00:55
5

Hashtable is used for fast lookup not for maintaining order. You should look into LinkedHashMap or other data structures.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 1
    LinkedHasMap is a very good Implementation when we also want the ordering in hashmap. I was trying to search for a good design or pseudo-code for the same but was not able to find any. If you are aware of any of the links then please share. – wayfare Jul 26 '11 at 00:29
4

LinkedHashMap is used for maintaining order of inserting elements.. Hashtable is similar to HashMap but it doesn't allow null key or value while HashMap allows one null key and several null values...

Akash5288
  • 1,919
  • 22
  • 16
3

From Map Javadoc.

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Also it's very useful to look inside the code of Hashtable and HashMap.

Donz
  • 1,389
  • 1
  • 13
  • 21