How to get all entries from LinkedHashMap in Java?

I am using a String object as the key to the associated hashmap.

How can I get all the entries in LinkedHashMap?

+3
source share
2 answers

You have three options that depend on whether you only need 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 to. Virtually all of them allow you to iterate using a simple foreach loop:

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

You can use a method entrySetthat returns a set containing key-value pairs on your map.

+4
source

Source: https://habr.com/ru/post/1774738/