检索List中的Map值

时间:2013-03-26 09:33:46

标签: java

ArrayList<HashMap<String, Object>> list = ArrayList<HashMap<String, Object>>()

我有一个包含hashmap的数组列表,我能够获取List的位置,现在我将如何获得List中对象的键and值。

@Override
public View getDropDownView() {        
    System.out.println(data.get(position)); // i am getting my List Objects 
}

//以下是data.get(position)

的输出
 {"title":"hello"}, => 0 
 {"title":"hello1"}, => 1
 {"title":"hello2"}, => 2
 {"title":"hello3"}, => 3

5 个答案:

答案 0 :(得分:2)

完整示例请尝试:

ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> map1 = new HashMap<String, Object>();
HashMap<String, Object> map2 = new HashMap<String, Object>();
map1.put("title", "hello");
map2.put("title2", "hello2");
list.add(map2);
list.add(map1);

HashMap<String, Object> innerMap;

         for(int i=0;i<list.size();i++)
         {
              innerMap = list.get(i);           

             for (Map.Entry<String, Object> entry : innerMap.entrySet())
             {
                 System.out.println(entry.getKey() + "/" + entry.getValue());
             }
         }

答案 1 :(得分:2)

试试这个:

List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

    for (Map element : list) {
        Set<Map.Entry<String, Object>> entrySet = element.entrySet();
        for (Map.Entry<String, Object> mapEntry : entrySet) {
            System.out.println("Key is : " + mapEntry.getKey());
            System.out.println("Value is : " + mapEntry.getValue());
        }
    }

答案 2 :(得分:2)

上述代码中的修改很少。请找到以下代码片段。

public class Test01 {
public static void main(String[] args) {
    ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map=new HashMap<String, Object>();
    map.put("test_key", "test_value");
    data.add(map);
    HashMap hashMap = data.get(0);
    Iterator<Object> iterator=hashMap.entrySet().iterator();
    while(iterator.hasNext()){
        Map.Entry<String, Object> entry=(Entry<String, Object>) iterator.next();
        System.out.println("Key :"+entry.getKey()+" Value : "+entry.getValue());
    }

}

}

我希望这可能有所帮助...

答案 3 :(得分:1)

你的问题还有很多不足之处,但我相信这就是你要找的东西

public class HashMapClass {

    public static void main(String[] args){

        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

        //Get the Hasmap at position
        HashMap map = data.get(position);

        //Get the data in a the hashmap
        Object obj = map.get(key);
    }
}

答案 4 :(得分:1)

您可以使用Map.Entry

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Y {

    public static void main(String[] args) {
        // Your data structure...
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        //Add some dummy data
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("1", "A");
        map.put("2", "B");
        map.put("3", "C");

        //Add the Map to the List
        list.add(map);

        int positionInList = 0; //Manipulate this how you want

        //Use Map.Entry to access both key and value
        for (Entry<String, Object> entry : list.get(positionInList).entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
        }
    }
}