java 在 HashMap 中迭代如何持续到第一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4237245/
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
How iteration last to first in HashMap?
提问by Mediator
I have HashMAp
我有 HashMAP
items = new HashMap<String, String>();
items.put("A", "1");
items.put("B", "2");
items.put("C", "3");
I need for each last to first.
我需要每个最后到第一个。
"C", "3"
"B", "2"
"A", "1"
采纳答案by iftheshoefritz
EDIT: Myself and Matthew clearly have different interpretations of what your question means. Do you mean reverse order that you inserted, or reverse order of keys?
编辑:我自己和马修显然对你的问题的含义有不同的解释。您的意思是插入的顺序相反,还是键的顺序相反?
If you mean reverse order of keys, here's how to do it:
如果您的意思是键的反向顺序,请按以下方法操作:
Use an ordered Map, like TreeMap and then iterate over items.keySet().
使用有序 Map,如 TreeMap,然后迭代 items.keySet()。
TreeMap sorts by natural order of your key values, so you will need to pass in a comparator to the constructor to sort the keys in reverse order:
TreeMap 按键值的自然顺序排序,因此您需要将比较器传递给构造函数以按相反顺序对键进行排序:
Map<String, String> items = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String a, String b) {
return b.compareTo(a);
}
});
items.put("A", "1");
items.put("B", "2");
items.put("C", "3");
for (String s: items.keySet()) {
System.out.println(s + " " + items.get(s));
}
回答by barjak
You can use a NavigableMap
(TreeMap
is a NavigableMap
), which is a SortedMap
with navigation capabilities.
您可以使用NavigableMap
( TreeMap
is a NavigableMap
),它是SortedMap
具有导航功能的。
NavigableMap#descendingMap()
returns a reverse order view (not a copy) of the mappings contained in this map.
NavigableMap#descendingMap()
返回此映射中包含的映射的逆序视图(不是副本)。
Example :
例子 :
NavigableMap<String, String> items = new TreeMap<String, String>();
items.put("B", "2");
items.put("A", "1");
items.put("C", "3");
for (Map.Entry<String, String> e : items.entrySet()) {
System.out.println(e);
}
// gives
// A=1
// B=2
// C=3
for (Map.Entry<String, String> e : items.descendingMap().entrySet()) {
System.out.println(e);
}
// gives
// C=3
// B=2
// A=1
Note : This answer is valid if you care about the natural orderingof the keys in your Map
. If you care about the insertion ordering or the access ordering, then have a look at LinkedHashMap
.
注意:如果你关心这个答案是有效的自然顺序在你的钥匙Map
。如果您关心插入顺序或访问顺序,请查看LinkedHashMap
.
Note 2 : In your question, you used a HashMap
. Please note that HashMap
doesn't guarantee any order for its elements. Actually, it doesn't even guarantee the order will remain constant over time. See the first paragraph of HashMap
's javadocfor further references.
注 2:在您的问题中,您使用了HashMap
. 请注意,HashMap
不保证其元素的任何顺序。实际上,它甚至不能保证订单会随着时间的推移保持不变。有关进一步参考,请参阅HashMap
的javadoc 的第一段。
回答by Matthew Flaschen
HashMap doesn't guarantee anyordering. If you use LinkedHashMap
, it will be ordered by insertion, but there is still no convenient way to go backwards.
HashMap 不保证任何顺序。如果使用LinkedHashMap
,则会按插入排序,但仍然没有方便的方法可以倒退。
One way would be to call items.entrySet()
. That returns a Set<Map.Entry>
. You can then get the size of the set, call toArray()
, then do a descending for loop.
一种方法是调用items.entrySet()
. 那返回一个Set<Map.Entry>
. 然后您可以获取集合的大小,调用toArray()
,然后执行降序 for 循环。
回答by sje397
Another method - create a SortedSet of your keys:
另一种方法 - 创建您的键的 SortedSet:
import java.util.*;
class MyComparator implements Comparator<String> {
public int compare(String a, String b) {
return -a.compareTo(b);
}
public boolean equals(String a, String b) {
return a.equals(b);
}
}
public class test {
public static void main(String[] args) {
HashMap<String, String> items = new HashMap<String, String>();
items.put("A", "1");
items.put("B", "2");
items.put("C", "3");
TreeSet<String> ts = new TreeSet<String>(new MyComparator());
ts.addAll(items.keySet());
for(Iterator<String> i = ts.iterator(); i.hasNext(); ) {
String key = i.next();
System.out.println("key: " + key + ", value: " + items.get(key));
}
}
}
output:
输出:
key: C, value: 3 key: B, value: 2 key: A, value: 1