List<Map<String, String>> java怎么遍历
java中遍历list可以使用keyset来遍历,示例如下:
Map<String,Object> map = new HashMap<String, String>();
map.put("1", "fds");
map.put("2", "valu");
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(map);
for (Map<String, String> m : list)
{
for (String k : m.keySet())
{
System.out.println(k + " : " + m.get(k));
}
}
for (int i=0;i<list.size(),i++)
{
Map map=(Map)list.get(i);
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
Object object = map.get(key);
}
}
List> 这样的!
List> list = new ArrayList>();
for(int i = 0 ; i < list.size(); i ++){
System.out.print(list.get(i)); // 查看单个Map中的数据
}
我在数据库里读出了 一条数据 里面有三个字段 我想取出这三个字段的值, 能给个例子吗?
public static void main(String[] args) {
List<Map> list = new ArrayList<Map>();
for (int i = 0; i < 2; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("operator_id", "admin_"+i);
map.put("name", "name_"+i);
list.add(map);
}
// 已完成List<Map<String,String>>的数据源
System.out.println(list);
// 获取单个的Map数据
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = list.get(i) ;
Set<String> set = map.keySet();
List<String> keyList = new ArrayList<String>();
keyList.addAll(set) ;
for (int j = 0; j < keyList.size(); j++) {
System.out.println("第"+j+"个Map数据");
System.out.println("key:" + keyList.get(j) + "---Value:" + map.get(keyList.get(j)));
}
}
}
不知道是不是你要的结果?