Java HashTable 维护插入顺序吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6405694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:39:53  来源:igfitidea点击:

Does HashTable maintains the insertion order?

javacollectionsdictionaryhashtable

提问by wayfare

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.

以下代码以相同的插入顺序为我提供了输出。我阅读了 javadoc,他们甚至没有谈论插入顺序。有人可以帮助我获得正确的信息。

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));
    }
}
}

采纳答案by Mechanical snail

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

不,不是的。要保留插入顺序,请改用java.util.LinkedHashMap( javadoc)。

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

此外,HashMap现在优先于Hashtable,因为Hashtable具有不必要的并发开销。(请参阅HashMap 和 Hashtable 之间的差异?。)

回答by jcomeau_ictx

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.

不,不是的。它只知道“哈希”顺序。如果您对字符串重新排序,您会发现它们仍然以哈希表中的相同顺序出现。

回答by fastcodejava

Hashtableis used for fast lookup not for maintaining order. You should look into LinkedHashMapor other data structures.

Hashtable用于快速查找而不是用于维护订单。您应该查看LinkedHashMap或其他数据结构。

回答by Donz

From Map Javadoc.

来自 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.

地图的顺序定义为地图集合视图上的迭代器返回其元素的顺序。一些地图实现,如 TreeMap 类,对它们的顺序做出特定保证;其他的,比如 HashMap 类,没有。

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

此外,查看 Hashtable 和 HashMap 的代码内部也非常有用。

回答by Akash5288

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...

LinkedHashMap 用于维护插入元素的顺序.. Hashtable 类似于 HashMap 但它不允许空键或值,而 HashMap 允许一个空键和多个空值......