简体   繁体   English

如何从Java中的LinkedHashMap获取所有条目?

[英]How to get all the entries from a LinkedHashMap in Java?

I am using String object as the key to a linkedhashmap. 我使用String对象作为linkedhashmap的关键。

How can I get all the entries in the LinkedHashMap? 如何获取LinkedHashMap中的所有条目?

You have three options that depend on whether you need just keys, just values or both 您有三个选项取决于您是只需要键,只需要值还是两者

Set<String> keys = yourMap.keySet();
Collection<YourValueClass> values = yourMap.values();
Set<Map.Entry<String,YourValueClass>> pairs = yourMap.entrySet();

then you can easily iterate over them if you need. 然后,如果需要,您可以轻松地迭代它们。 Actually all of them allow iterating using a simple foreach loop: 实际上所有这些都允许使用简单的foreach循环进行迭代:

for (Map.Entry<String,YourClassValue> e : yourMap.entrySet())
  // do something

您可以使用entrySet方法,该方法返回包含地图中键值对的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM