Java 如何获取哈希图的第一个键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35658606/
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 to get the first key of a hashmap?
提问by Jürgen K.
I have the following HashMap<String, Double> map = new HashMap<String, Double>();
我有以下 HashMap<String, Double> map = new HashMap<String, Double>();
How can i get the first key without iterating over it like this:
我怎样才能获得第一个键而不像这样迭代它:
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove();
}
Thanks
谢谢
采纳答案by SkyWalker
To get the value of the "first" key, you can use it
要获取“第一个”键的值,您可以使用它
map.get(map.keySet().toArray()[0]);
In Java8,
在 Java8 中,
You can use stream. For TreeMap/LinkedHashMap, where ordering is significant, you can write
您可以使用流。对于TreeMap/LinkedHashMap,排序很重要,你可以写
map.entrySet().stream().findFirst();
For HashMap, there is no order, so findAny()
might return a different result on different calls
对于HashMap,没有顺序,因此findAny()
可能会在不同的调用中返回不同的结果
map.entrySet().stream().findAny();
回答by Rehman
Map<String, Double> map=new HashMap<>();
Map.Entry<String, Double> entry=map.entrySet().iterator().next();
String key= entry.getKey();
But HashMap doesn't maintain insertion order. You can use LinkedHashMapinstead of HashMap.
但是 HashMap 不维护插入顺序。您可以使用LinkedHashMap代替 HashMap。
回答by José María
Just call it.next()
once, be sure the iterator is just in the first position.
只调用it.next()
一次,确保迭代器刚好在第一个位置。
Iterator it = map.entrySet().iterator();
Map.Entry firstEntry = (Map.Entry)it.next();
回答by Evan LaHurd
If you want to iterate over the Map
and expect to iterate over it in the order you inserted entries, you should use a LinkedHashMap
instead of a HashMap
.
如果您想遍历Map
并希望按照插入条目的顺序遍历它,则应使用 aLinkedHashMap
而不是 a HashMap
。
This is assuming "first key" means the first one you inserted. Then you can iterate over it in the order you inserted entries, or if you just want the first key like you said, you can call map.keySet().toArray()[0]
.
这是假设“第一个键”表示您插入的第一个键。然后你可以按照你插入条目的顺序迭代它,或者如果你只想要你说的第一个键,你可以调用map.keySet().toArray()[0]
.
回答by RIP_SunMicroSys
Try using LinkedHashMap instead of HashMap, It maintains the order of insertion when calling keySet().
尝试使用 LinkedHashMap 而不是 HashMap,它在调用 keySet() 时保持插入的顺序。
回答by MartinS
Since your question is not very specific about what you consider the "first key" I will just list a few options.
由于您的问题对于您认为的“第一把钥匙”不是很具体,我将仅列出几个选项。
Just the first one in the key set
只是密钥集中的第一个
String firstKey = map.keySet().iterator().next();
But no idea what information that provides you.
但不知道为您提供了哪些信息。
The smallest key
最小的钥匙
String firstKey = map.keySet().stream().min(String::compareTo).get();
The key of the smallest value
最小值的键
String firstKey = map.entrySet().stream().min((a,b) -> a.getValue().compareTo(b.getValue())).get().getKey();
The first inserted key
第一个插入的密钥
This does not work with a regular HashMap
because it does not preserve the ordering. Use a LinkedHashMap
instead.
这不适用于常规,HashMap
因为它不保留顺序。使用 aLinkedHashMap
代替。
Map<String, Double> map = new LinkedHashMap<>();
String firstKey = map.keySet().iterator().next();
回答by karthik r
if you use Java 8,
如果您使用 Java 8,
map.entrySet().stream().findFirst().get().getKey()