user2939293
user2939293

Reputation: 813

Get values from List<Map<String, String>>

I have a problem with my java code that I hope someone can help me with.

I have a list of type List<Map<String, String>> which I populate using this code:

List<Map<String, String>> myList = new ArrayList<Map<String, String>>();

for (int i=0; i<daysList.getLenght(); i++)
{
    Map<String, String> map = new HashMap<String, String>();
    map.put(value1, value2);
    myList.add(map);  
}

Now I want to get the values from myList. I try this, but it is not working. I can somehow see that it wouldn't but can't figure out how it should be.

for (int j=0; j<myList.size(); j++)
{
    String val1 = myList.get("value1");
    String val2 = myList.get("value2");
}

I appreciate your time and help.

Upvotes: 1

Views: 49148

Answers (8)

IQ.feature
IQ.feature

Reputation: 658

Short example :

Map<String, String> map = new HashMap<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}

Upvotes: 0

Nauman Ahmad
Nauman Ahmad

Reputation: 320

i have added two maps to List.

List<Map<String, String>> myList = new ArrayList<Map<String, String>>();

Map<String, String> map = new HashMap<String, String>();
map.put("hello", "value");
map.put("hello2", "value2");
map.put("hello3", "value3");
map.put("hello4", "value4");

Map<String, String> map2 = new HashMap<String, String>();
map2.put("hello5", "value5");
map2.put("hello6", "value6");
map2.put("hello7", "value7");
map2.put("hello8", "value8");      
myList.add(map);  
myList.add(map2);
Map<String, String> mymap = new HashMap<String, String>();  

for (int j=0; j<myList.size(); j++)
{
    // Key set of map(j) has been retrieved here
    Set<String> val1 = myList.get(j).keySet();

    // Used iterator to loop over each map key to get respective value
    Iterator<String> it = val1.iterator();
    while(it.hasNext()){
        String next=it.next();
        String x= myList.get(j).get(next);
        mymap.put(next,x);
    }
}
// *Put any key over here and it will give value for that key.*

String mystring=mymap.get("hello4");
System.out.println(mystring);

Upvotes: 2

user2195058
user2195058

Reputation: 927

Lets track it down:

The way you have initiated:

List<Map<String, String>> myList = new ArrayList<Map<String, String>>();

So you have a list of maps.

Now how do we get an item from a List, there are two ways:

for(int index = 0 ; index < myList.size() ; index++){
    Map<String, String> listItem = myList.get(index);
    // Iterate over the map.
}

or

for(Map<String, String> listItem : myList){
    // Iterate over the map.
}

Now how do we iterate over the map:

Iterator it = listItem.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
}

Upvotes: 4

Prashanth Peddabbu
Prashanth Peddabbu

Reputation: 92

list of maps

I hope this solution gives the clear picture of the things that you are doing(or trying to do) with your program.

Upvotes: 0

Manikant Gautam
Manikant Gautam

Reputation: 3591

if the below statement

 map.put(value1, value2);

is true . then you could use like this

 map containerMap=new HashMap<String,String>();
   String val1="";  
 for (int j=0; j<myList.size(); j++)
 {
                   containerMap = mylist.get(j);
     val1 = containerMap.get(value1);
 }

Upvotes: 0

you have a list of maps so every element in the list is a maP :)

you need to get 1st the element in the list, and then work with them as a map object:

Example:

List<Map<String, String>> myList = new ArrayList<Map<String, String>>();
Map<String, String> myMap = new HashMap<String, String>();
// populate
for (int i = 0; i < 3; i++) {
    myMap.put("key", "val+" + i);
    myList.add(myMap);
}
// retrieve
for (int i = 0; i < myList.size(); i++) {
    System.out.println("my value is: "+myList.get(i).get("myKey"));
}

Upvotes: 1

Dimitrios Begnis
Dimitrios Begnis

Reputation: 813

You've put a Map into a List so with myList.get() you can only get the Map not the values.

In your example you don't need a List. You can just use a HashMap

Map<String,String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");

Now map.get("key1"); will return "value1"

Upvotes: 2

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

You need to get your map from the list before getting the values out of your map. Something like below :

Map<String, String> myMap ; 

for (int j=0; j<myList.size(); j++)
{
    myMap = mylist.get(i);

     String val1 = myMap.get("value1");
     String val2 = myMap.get("value2");

}

Upvotes: 2

Related Questions