简体   繁体   中英

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

I am using String object as the key to a linkedhashmap.

How can I get all the entries in the 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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