Get values from List<map<string, string="">&gt;

时间:2016-04-04 18:20:10

标签: java

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.

8 个答案:

答案 0 :(得分:2)

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");

}

答案 1 :(得分:2)

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"

答案 2 :(得分:1)

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());
}

答案 3 :(得分:1)

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"));
}

答案 4 :(得分:1)

  

我已将两张地图添加到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);

答案 5 :(得分:0)

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);
 }

答案 6 :(得分:0)

list of maps

我希望这个解决方案可以清楚地了解您正在使用您的程序做什么(或尝试做什么)。

答案 7 :(得分:0)

简短示例

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