2

New to using map and was wondering how to add values and loop thur it to retrieve the values. Below is my code:

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    map.put(categoryName , null);
}

for which i will be having this which is im assuming is correct even with the null

MAP {5=null, 11=null, 15=null, 24=null, 96=null, 98=null}

Its currently null as the next process (below) will fill up that list as it loops thru a for condition unless it can be done to add the categoryName and the List values at the same time?

Now, i need to loop and add a list from each string/map

String value1 = value;
String value2 = value;

for (Entry<String, List<String>> entry : map.entrySet()) {
    map.put(value1, entry.getValue());  --> doesn't work
    map.put(value2, entry.getValue());  --> doesn't work
}

I need something like this

MAP {5=[value1,value2,etc], 11=[value1,value2,etc], .......

Problem is, it doesn't work I can't seem to add inside the List. I need help on how to add values from a map > and loop thru to it?

5
  • The map contains null as values and no list, why not start there? Commented Jul 1, 2014 at 6:33
  • Do you want to add map values to List ? Commented Jul 1, 2014 at 6:38
  • Can you modify MAP {5=null, 11=null, 15=null, 24=null, 96=null, 98=null} with your example values instead of null? It is very confusing.. as you are expecting MAP {5=[value1,value2,etc], 11=[value1,value2,etc], ....... and not null values.. Commented Jul 1, 2014 at 6:41
  • Its currently null as the next process (below) will fill up that list as it loops thru a for condition unless it can be done to add the categoryName and the List values at the same time? Commented Jul 1, 2014 at 6:46
  • Have a look at Guava's Multimap. Commented Jul 1, 2014 at 6:47

3 Answers 3

1

This is what you need:

anotherMap.forEach((k, v) -> myMap.put(k.toString(), v.toString()));

Source: Looping Through a Map in Java

Sign up to request clarification or add additional context in comments.

Comments

0

Use Iterator insted of EntrySet

    Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
    Iterator itr=map.keySet().iterator();
    while (itr.hasNext()) {
        String key =  itr.next().toString();
        String value=map.get(key).toString();
        System.out.println(key+"="+value);
    }

Comments

0

When you create your map, instead of filling it with nulls, fill it with empty Lists:

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    map.put(categoryName , new ArrayList<String>());
}

Now, when you're ready to populate the values, you can do:

String value1 = value;
String value2 = value;

for (List<String> list : map.values()) {
    list.add(value1);
    list.add(value2);
}

If you have the values when you're creating the map, you make it simpler:

String value1 = value;
String value2 = value;

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    List<String> values = new ArrayList<String>();
    values.add(value1);
    values.add(value2);
    map.put(categoryName, values);
}

Comments

Your Answer

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.