java 排序的哈希图迭代

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

Sorted hashmap iteration

java

提问by nKognito

I have such code:

我有这样的代码:

HashMap<Long, TempPosition> positions = getTempPositions();
    for (SortedMap.Entry<Long, TempPosition> p: positions.entrySet()) {...}

The problem is the 'positions' is not sorted or non valid sorted. What the easiest way to iterate through the hashmap and save its current order?

问题是“位置”未排序或排序无效。遍历哈希图并保存其当前顺序的最简单方法是什么?

Thank you

谢谢

回答by Till Helge

A HashMapby definition doesn't have an order. If you need to preserve or create some kind of order you need to use TreeMapinstead of HashMap.

AHashMap根据定义没有顺序。如果您需要保留或创造某种秩序您需要使用TreeMap代替HashMap

回答by JB Nizet

A HashMap doesn't have any order. If you want insertion order, use a LinkedHashMap. If you want keys sorted using their natural ordering or a custom comparator, use a TreeMap.

HashMap 没有任何顺序。如果您想要插入顺序,请使用 LinkedHashMap。如果您希望使用自然顺序或自定义比较器对键进行排序,请使用 TreeMap。

回答by Peter Lawrey

HashMap doesn't have an order. You can't even guarentee that two HashMaps with the same keys will have the same order.

HashMap 没有顺序。您甚至无法保证具有相同键的两个 HashMap 将具有相同的顺序。

If you want an order using TreeMapor LinkedHashMapand the iterator will be in the order the collection provides.

如果您想要使用TreeMapor的顺序,LinkedHashMap并且迭代器将按照集合提供的顺序排列。



Note: In some situations the keys will be sorted, so the keys are not even guarenteed to be random.

注意:在某些情况下,键会被排序,所以键甚至不能保证是随机的。

HashMap<Integer, String> map = new HashMap<>();
for(int i=0;i<10;i++)
    map.put(i, ""+i);
System.out.println(map.keySet());

prints

印刷

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

回答by Alberto

You can use an implementation of java.util.SortedMap, like java.util.TreeMapinstead of HashMap.

您可以使用java.util.SortedMap的实现,例如java.util.TreeMap而不是 HashMap。

回答by Sashi Kant

 SortedSet<String> sortedset= new TreeSet<String>(positions.keySet());

    Iterator<String> it = sortedset.iterator();

    while (it.hasNext()) {
      System.out.println (it.next());
    }

回答by Aaron Digulla

Since the key can be sorted, this approach is the most simple:

由于key是可以排序的,这个方法最简单:

HashMap<Long, TempPosition> positions = getTempPositions();
for (Map.Entry<Long, TempPosition> p: 
    new TreeMap<Long, TempPosition>( positions ).entrySet()) {...}

The trick is to wrap the map in a TreeMap. Note that this doesn't work (well) if the map is huge.

诀窍是将地图包装在TreeMap. 请注意,如果地图很大,这将不起作用(很好)。

回答by adaslaw

Here you have a sample code using streams and classic foreach loop:

这里有一个使用流和经典 foreach 循环的示例代码:

HashMap<String,String> map = new HashMap<>();
map.put("c", "C");
map.put("a", "A");
map.put("b", "B");

for (Map.Entry<String,String> e : (Iterable<Map.Entry<String,String>>) map.entrySet().stream().sorted(Map.Entry.comparingByKey())::iterator) {
    System.out.println("key:" + e.getKey() + ", val: " + e.getValue());
}