3

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

How can I get all the entries in the LinkedHashMap?

asked Nov 14, 2010 at 0:48

2 Answers 2

9

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
Sean Patrick Floyd
300k72 gold badges480 silver badges597 bronze badges
answered Nov 14, 2010 at 0:53
1
  • simple and beautiful answer (+1) Commented Nov 14, 2010 at 9:27
4

You can use the entrySet method, which returns a set containing the key-value pairs in your map.

answered Nov 14, 2010 at 0:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.