65

I have following LinkedHashMap declaration.

LinkedHashMap<String, ArrayList<String>> test1

my point is how can i iterate through this hash map. I want to do this following, for each key get the corresponding arraylist and print the values of the arraylist one by one against the key.

I tried this but get only returns string,

String key = iterator.next().toString(); 
ArrayList<String> value = (ArrayList<String> )test1.get(key)
Craig P. Motlin
26.8k18 gold badges104 silver badges128 bronze badges
asked Sep 7, 2012 at 2:24
1
  • LinkedHashMap is used for preserving ordering. The question, nor the responses are mentionning ordering. Please replace LinkedHashMap by HashMap. This is very confusing Commented May 13, 2020 at 7:49

5 Answers 5

160
for (Map.Entry<String, ArrayList<String>> entry : test1.entrySet()) {
 String key = entry.getKey();
 ArrayList<String> value = entry.getValue();
 // now work with key and value...
}

By the way, you should really declare your variables as the interface type instead, such as Map<String, List<String>>.

answered Sep 7, 2012 at 2:28
6
  • 3
    btw, I want to have the list with the insertion order, i used hashmap before but it messed up the order. Commented Sep 7, 2012 at 2:34
  • 4
    I'm not saying don't use LinkedHashMap - but typically best practice is to declare things like Map<String, List<String>> map = new LinkedHashMap<String, List<String>>. Commented Sep 7, 2012 at 2:36
  • 32
    Why is the order guaranteed when entrySet returns a set? I know order is guaranteed in the LinkedHashMap, but the docs say the iterator of a set makes no guarantee on order Commented Dec 7, 2014 at 13:31
  • 2
    Why did you keep ArrayList in the declarations, then? Commented Jan 5, 2016 at 13:42
  • 3
    @Jonathan., good question. Better late than never: see the answer at stackoverflow.com/a/2924143/423105 Commented Jun 2, 2016 at 22:04
15

I'm assuming you have a typo in your get statement and that it should be test1.get(key). If so, I'm not sure why it is not returning an ArrayList unless you are not putting in the correct type in the map in the first place.

This should work:

// populate the map
Map<String, List<String>> test1 = new LinkedHashMap<String, List<String>>();
test1.put("key1", new ArrayList<String>());
test1.put("key2", new ArrayList<String>());
// loop over the set using an entry set
for( Map.Entry<String,List<String>> entry : test1.entrySet()){
 String key = entry.getKey();
 List<String>value = entry.getValue();
 // ...
}

or you can use

// second alternative - loop over the keys and get the value per key
for( String key : test1.keySet() ){
 List<String>value = test1.get(key);
 // ...
}

You should use the interface names when declaring your vars (and in your generic params) unless you have a very specific reason why you are defining using the implementation.

answered Sep 7, 2012 at 2:32
2
  • 1
    Hi i used linkhashmap to keep the elements in insertion order. Commented Sep 7, 2012 at 2:35
  • Sure - but you can specify LinkedHashMap when creating the instance. However, using the interface name for the variable allows you to make the code implementation independent. ie: you can easily replace the implementation with something else at a later point without needing to recode everything. See my example above for the declaration using the interface as the var, and the LinkedHashMap for the implementation. Commented Sep 7, 2012 at 2:38
13

In Java 8:

Map<String, List<String>> test1 = new LinkedHashMap<String, List<String>>();
test1.forEach((key,value) -> {
 System.out.println(key + " -> " + value);
});
answered Apr 22, 2017 at 22:27
8

You can use the entry set and iterate over the entries which allows you to access both, key and value, directly.

for (Entry<String, ArrayList<String>> entry : test1.entrySet()) {
 System.out.println(entry.getKey() + "/" + entry.getValue());
}

I tried this but get only returns string

Why do you think so? The method get returns the type E for which the generic type parameter was chosen, in your case ArrayList<String>.

answered Sep 7, 2012 at 2:28
2
  • Hey, There's a missing bracket after test1.entrySet(), but since the edit is shorter than 6 characters I cannot make it, just wanted to point that out for future users Commented Oct 31, 2021 at 9:23
  • 1
    I updated it, thank you. Commented Nov 16, 2021 at 6:08
7
// iterate over the map
for(Entry<String, ArrayList<String>> entry : test1.entrySet()){
 // iterate over each entry
 for(String item : entry.getValue()){
 // print the map's key with each value in the ArrayList
 System.out.println(entry.getKey() + ": " + item);
 }
}
answered Sep 7, 2012 at 2:28
1
  • 3
    Never thought of this, i used foreach in C#, this is similar to that. Commented Sep 7, 2012 at 2:32

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.