取出List>里面Map的key:value值

时间:2022-06-15 17:52:00

 

1.取出Map其中一个属性的值

Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
List> list = new ArrayList>();
list.add(map);
for(int i = 0;i < list.size();i++)
{
    Map<String,Object> map = list.get(i);
    String authorStr = map.get("key1").toString();
    System.out.println("author" + " : " + authorStr);
}

2.取出所有的key:value

//第一种:
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
<List> list = new ArrayList>();
list.add(map);
for (Map m : list)
{
    for (String k : m.keySet())
    {
        System.out.println(k + " : " + m.get(k));
    }
}

//第二种:
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
<List> list = new ArrayList>();
list.add(map);
for(Map map:list)
{
    for(Map.Entry e:map)
    {
        e.getKey() 和 e.getValue() 就是你想要的
    }
}

//第三种:
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
<List> list = new ArrayList>();
list.add(map);
for (int i=0;i<list.size();i++)
{
    Map  map1=(Map)list.get(i);
    Iterator iterator = map1.keySet().iterator();
    while (iterator.hasNext())
    {
        String key = (String) iterator.next();
        Object object = map1.get(key);
        System.out.println(object);
    }
}