HashTable 是否维护插入顺序?

发布于 2024-11-16 05:24:40 字数 898 浏览 1 评论 0 原文

下面的代码以相同的插入顺序给出了输出。我读了 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));
    }
}
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-11-23 05:24:40

不,事实并非如此。要保留插入顺序,请改用 java.util.LinkedHashMap (javadoc)。

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

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

听风吹 2024-11-23 05:24:40

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

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.

清醇 2024-11-23 05:24:40

Hashtable 用于快速查找,而不是用于维护顺序。您应该研究 LinkedHashMap 或其他数据结构。

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

冰魂雪魄 2024-11-23 05:24:40

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

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

没︽人懂的悲伤 2024-11-23 05:24:40

来自地图 Javadoc。

映射的顺序定义为映射集合视图上的迭代器返回其元素的顺序。一些映射实现(例如 TreeMap 类)对其顺序做出了特定保证;其他类(例如 HashMap 类)则不然。

查看 Hashtable 和 HashMap 的代码也非常有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文