java 获取linkedhashmap的第一项

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

Get the first item of linkedhashmap

javacollectionslinkedhashmap

提问by PrabhaT

I am using LinkedHashMap. I will always process the first value and that can be deleted (if possible) so that during the next iteration I will again take the same first value from the map to process. What can I use to get the first value.

我正在使用LinkedHashMap. 我将始终处理第一个值并且可以将其删除(如果可能),以便在下一次迭代期间我将再次从地图中获取相同的第一个值进行处理。我可以用什么来获得第一个值。

回答by krock

You can use this to get the first element key:

您可以使用它来获取第一个元素键:

 Object key = linkedHashMap.keySet().iterator().next();

then to get the value:

然后获取值:

Object value = linkedHashMap.get(key);

and finally to remove that entry:

最后删除该条目:

linkedHashMap.remove(key);

回答by amaidment

Use the an Iterator on the value set - e.g.

在值集上使用迭代器 - 例如

Map map = new LinkedHashMap();
map.put("A", 1);
map.values().iterator().next();

From your question, it's not clear to me that a map is the best object to use for your current task.

根据您的问题,我不清楚地图是用于您当前任务的最佳对象。

回答by BeCodeMonkey

If you are going to require the value and key it is best to use the EntrySet.

如果您需要值和键,最好使用 EntrySet。

LinkedHashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
Entry<Integer, String> mapEntry = map.entrySet().iterator().next();
Integer key = mapEntry.getKey();
String value = mapEntry.getValue();