簡體   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