繁体   English   中英

在List中重新映射Map的值

[英]Retriving the values of Map inside List

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

完整示例请尝试以下操作:

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());
             }
         }

试试这个:

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());
        }
    }

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

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());
    }

}

}

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

你的问题还有很多不足之处,但我相信这正是你所寻找的

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);
    }
}

您可以使用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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM