-5

I am modifying someone else code. I have a variable of type List<HashMap<String, String>>

List<HashMap<String, String>> lst

I tried to use get the item single value by using

lst[0,1]
lst[0][1]
lst.get(0)[1]
lst.get(0)(1)
lst.get(0)("ID")

but non works.

how to get a single item value?

    List<HashMap<String, String>> lst = null;

    try{
        lst = myXmlParser.detparse(reader);
    }catch(Exception e){
        Log.d("Exception", e.toString());
    }
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • use `list.get(index);` it will return you an object of type `HashMap` `index` here is an `int` of your choice. – SMR Jul 07 '14 at 09:43
  • Should be `List> lst` – EpicPandaForce Jul 07 '14 at 09:46
  • I used list.get(index) but it will not return a single value it will return a list of 4 items – asmgx Jul 07 '14 at 09:46
  • possible duplicate of [Java: get specific ArrayList item](http://stackoverflow.com/questions/3920602/java-get-specific-arraylist-item) – SMR Jul 07 '14 at 09:48
  • Not sure what the code producing the List> by getparse is meant to say in connection with the question "how to get a single item". – laune Jul 07 '14 at 09:57

4 Answers4

1

You should read the documentation of List and Map (respective HashMap) to see how to use them.

Since your List contains Map you'll have to get a Map using the List operations and then get the elements from that Map using the Map operations:

HashMap<String, String> firstMap = lst.get(0);
String someEntry = firstMap.get("key");
André Stannek
  • 7,773
  • 31
  • 52
0
HashMap<String, String> firstEntryInList = list.get(0);

Where '0' is the index.

Stu Whyte
  • 758
  • 5
  • 19
0

Why not using index of list...

HashMap<String, String> entry = lst.get(0);
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • @DownVoter, Can it be commented, why to down vote? To return an element from a list using indexes - use get(index) is the suggestion and its appropriate to the question. – Vinay Veluri Jul 07 '14 at 09:59
  • I think that a rather weird one was downvoting each and every answer on this Q. I got credited +8, which looks like one up and one down, others may have experienced similar. – laune Jul 07 '14 at 11:00
0

Accessor methods must be applied from outer to inner. If you have a

List<Whatever> lst

you use

lst.get(0)

to retrieve the first element in the list. If Whatever is in turn an aggregate, you apply the appropriate accessor on the returned value:

List<Map<String,String>> lst

and to get the string that's mapped via "somekey":

lst.get(0).get("somekey")

Now, this will get you a String, and this has accessors, too, eg. for accessing a single character:

lst.get(0).get("somekey").charAt(0)

This is "the first character of the string mapped with key "somekey" in the first hash map of the list lst".

laune
  • 31,114
  • 3
  • 29
  • 42