java 如何从迭代器()以正确的顺序获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6996191/
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-10-30 18:11:38  来源:igfitidea点击:

How to get the elements in correct order from iterator()

javaiteratorhashmap

提问by Santhosh

Here is my code to store the data into HashMap and display the data using iterator

这是我将数据存储到 HashMap 并使用迭代器显示数据的代码

public static void main(String args[]) {
    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("aaa", "111");
    hm.put("bbb", "222");
    hm.put("ccc", "333");
    hm.put("ddd", "444");
    hm.put("eee", "555");
    hm.put("fff", "666");

    Iterator iterator = hm.keySet().iterator();

    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        String val = hm.get(key);

        System.out.println(key + " " + val);
    }
}

But it is not displaying in the order in which I stored. Could someone please tell me where am I going wrong? How can I get the elements in the order?

但它没有按照我存储的顺序显示。有人可以告诉我我哪里出错了吗?如何获取订单中的元素?

回答by Jacob

A HashMaphas no guaranteed order:

一个HashMap中已没有保证的顺序:

This class makes no guarantees as to the order of the map;

此类不保证地图的顺序;

Use a LinkedHashMap.

使用LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order.

Map接口的哈希表和链表实现,迭代顺序可预测。

回答by dogbane

You need to use a LinkedHashMapbecause it maintains ordering of its entries, unlike HashMap.

您需要使用LinkedHashMap,因为与 HashMap 不同,它维护其条目的顺序。

From the javadocs:

从javadocs:

... implementation of the Map interface with predictable iteration 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).

...具有可预测迭代顺序的 Map 接口的实现。此实现与 HashMap 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,这通常是键被插入到映射中的顺序(插入顺序)。

回答by Android Killer

HashMap does not keep order in which we put data into it.So You may follow LinkedHashMap instead.It keeps the order in which we put data.LinkedHashMap can be used same as HashMap.

HashMap 不保持我们放入数据的顺序,所以你可以按照LinkedHashMap 代替。它保持我们放入数据的顺序。LinkedHashMap 可以和HashMap 一样使用。

Map<key,value> map=new LinkedHashMap<key,value>();
map.put("key","value");
map.put("key","value");
map.put("key","value");

//similarly you can use iterator to access data too.It will display dfata in order in which you added to it.

//同样的,你也可以使用迭代器来访问数据。它会按照你添加的顺序显示ddata。

回答by Andrey Krot

The reason is HashMap and HashSet doesn't guarantee the order of the stored values. Position of the elements will depends on the size of the internal table and hashCode of the key.

原因是 HashMap 和 HashSet 不保证存储值的顺序。元素的位置将取决于内部表的大小和键的 hashCode。

If you want to load your data in some order you need to sort keys/or values. For example you can put collections of the entries (Map.entrySet()) to the list and sort by any criteria you want. Or you can use SortedMap (TreeMap for example) to store your objects.

如果要按某种顺序加载数据,则需要对键/或值进行排序。例如,您可以将条目的集合 (Map.entrySet()) 放入列表并按您想要的任何条件排序。或者您可以使用 SortedMap(例如 TreeMap)来存储您的对象。