Reputation: 15034
ArrayList<HashMap<String, Object>> list = ArrayList<HashMap<String, Object>>()
I have an array list which contains an hashmap, i am able to get the position of my List , now how would i get the key and
value of the object in my List.
@Override
public View getDropDownView() {
System.out.println(data.get(position)); // i am getting my List Objects
}
// The below is the output of data.get(position)
{"title":"hello"}, => 0
{"title":"hello1"}, => 1
{"title":"hello2"}, => 2
{"title":"hello3"}, => 3
Upvotes: 3
Views: 8904
Reputation: 62
Little modification in above code.Please find the below code snippets.
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());
}
}
}
i hope this may be help...
Upvotes: 2
Reputation: 7804
Try this out :
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());
}
}
Upvotes: 3
Reputation: 2034
You could use 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();
}
}
}
Upvotes: 1
Reputation: 6158
With full example try this:
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());
}
}
Upvotes: 2
Reputation: 3996
Your question leaves much to be desired, but I believe this is what you're looking for
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);
}
}
Upvotes: 1