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
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
-
simple and beautiful answer (+1)Sean Patrick Floyd– Sean Patrick Floyd11/14/2010 09:27:13Commented Nov 14, 2010 at 9:27
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
lang-java