我的 java 代码有问题,希望有人能帮助我。
我有一个 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);
}
现在我想从 myList 中获取值。我试试这个,但它不起作用。我能以某种方式看到它不会,但无法弄清楚它应该如何。
for (int j=0; j<myList.size(); j++)
{
String val1 = myList.get("value1");
String val2 = myList.get("value2");
}
非常感谢您抽出时间提供帮助。
最佳答案
让我们追踪它:
你发起的方式:
List<Map<String, String>> myList = new ArrayList<Map<String, String>>();
所以你有一个 map 列表。
现在我们如何从一个列表中获取一个项目,有两种方法:
for(int index = 0 ; index < myList.size() ; index++){
Map<String, String> listItem = myList.get(index);
// Iterate over the map.
}
或
for(Map<String, String> listItem : myList){
// Iterate over the map.
}
现在我们如何遍历 map :
Iterator it = listItem.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
关于java - 从 List<Map<String, String>> 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410003/