简体   繁体   English

从列表中获取值<Map<String, String> &gt;

[英]Get values from List<Map<String, String>>

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>> ,我使用以下代码填充它:

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.现在我想从 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.我感谢您的时间和帮助。

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

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

}

You've put a Map into a List so with myList.get() you can only get the Map not the values.您已将Map放入List因此使用myList.get()您只能获取Map而不是值。

In your example you don't need a List .在您的示例中,您不需要List You can just use a HashMap你可以只使用一个HashMap

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

Now map.get("key1");现在map.get("key1"); will return "value1"将返回"value1"

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

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

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

地图列表

I hope this solution gives the clear picture of the things that you are doing(or trying to do) with your program.我希望这个解决方案能清楚地说明你正在用你的程序做(或试图做)的事情。

Short example :简短示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM