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

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

@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
4

5 回答 5

3

试试这个:

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());
        }
    }
于 2013-03-26T09:49:02.460 回答
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());
             }
         }
于 2013-03-26T09:43:56.253 回答
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());
    }

}

}

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

于 2013-03-26T09:56:56.683 回答
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);
    }
}
于 2013-03-26T09:40:58.223 回答
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();
        }
    }
}
于 2013-03-26T09:46:37.143 回答