java 从 List<Map<String, String>> 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36410003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:24:59  来源:igfitidea点击:

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

java

提问by user2939293

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

我的 java 代码有问题,希望有人能帮助我。

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

我有一个List<Map<String, String>>使用以下代码填充的类型列表:

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.

现在我想从 myList 中获取值。我试试这个,但它不起作用。我可以以某种方式看到它不会但无法弄清楚它应该如何。

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

I appreciate your time and help.

我感谢您的时间和帮助。

回答by Dimitrios Begnis

You've put a Mapinto a Listso with myList.get()you can only get the Mapnot the values.

您已将 aMap放入 a 中,List因此myList.get()您只能获得而Map不是值。

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

在您的示例中,您不需要List. 你可以只使用一个HashMap

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

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

现在map.get("key1");会回来"value1"

回答by Rajesh Pantula

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

}

回答by user2195058

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:

现在我们如何从 List 中获取一个 item,有两种方法:

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

回答by ΦXoc? ? Пepeúpa ツ

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

回答by Nauman Ahmad

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

回答by Prashanth Peddabbu

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.

我希望这个解决方案能够清楚地说明你正在用你的程序做(或试图做)的事情。

回答by manikant gautam

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