1

I have an list of arraylist of hashmap as below. Could you please help me on how loop through it inorder to get the key and compare the key with particular data, if it matches display its respective value.

List<ArrayList<HashMap>> li = new ArrayList<ArrayList<HashMap>>();
    li = SyncSettings.labelSharedInstance;

Below is the list of arraylist of hashmap storage view (and for reference i have added xml as well).

enter image description here

<labels>
        <label id="lblMS">
            <text language="EN" value="Morning Sync"/>
            <text language="DE" value="Morgan Sync"/>
        </label>
        <label id="lblES">
            <text language="EN" value="Evening Sync"/>
            <text language="DE" value="Sync Abend"/>
        </label>
        <label id="lblAS">
            <text language="EN" value="Afternoon Sync"/>
            <text language="DE" value="Sync Afternoon"/>
        </label>
    </labels>

Initially, i will be getting the device language and i need to check the device language with language in the hashmap. If it matches i need to display it respective language.

So far what i have achieved is as below.

for (ArrayList<HashMap> entry : li) {
        Set<String> keys = entry.keySet();
        for (String key : keys) {
         //I'm just seeing whether i'm doing in a right way or not. So i was just printing.But i was getting nothing 
            System.out.println("key = " + key);
            System.out.println("value = " + value);
        }

Could you please help me to proceed further. Thank you.

4 Answers 4

1

Assuming provided xml, following data structure can be created:

    List<ArrayList<HashMap<String, String>>> listSuper = new ArrayList<>();//List of label in your xml

    ArrayList<HashMap<String, String>> listInner = new ArrayList<>();//Correspond to a label in your xml

    HashMap<String, String> hashMapLblMs = new HashMap<>();
    hashMapLblMs.put("EN", "Morning Sync");
    hashMapLblMs.put("DE", "Morgan Sync");
    listInner.add(hashMapLblMs);

    HashMap<String, String> hashMaplblES = new HashMap<>();
    hashMaplblES.put("EN", "Evening Sync");
    hashMaplblES.put("DE", "Sync Abend");
    listInner.add(hashMaplblES);

    listSuper.add(listInner);

Now we can iterate in this by using for loop:

for (int i = 0; i < listSuper.size(); i++) {
        ArrayList<HashMap<String, String>> listInner1 = listSuper.get(i);
        for (HashMap<String, String> map : listInner1) {
            for (Map.Entry<String, String> entrySet : map.entrySet()) {
                System.out.println("key = " + entrySet.getKey());
                System.out.println("value = " + entrySet.getValue());
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@subhash.Hello Subhash, Thanks for replying. As per my list, Key is labelId-->lblMS and value-->language="EN" value="Morning Sync" as per my hashmap. Let me say, i pass label as paramter from outside, if the passed label id matches the key's labelid, i need to fetch the value. How can i achieve this?
1
    ArrayList<HashMap<String, String>> arrayList;
    ArrayList<String> lblMSList, lblASList, lblESList;
    arrayList = new ArrayList<HashMap<String, String>>();

    lblMSList= new ArrayList<String>();
    lblASList= new ArrayList<String>();
    lblESList= new ArrayList<String>();

    arrayList = getAllData();

    if (arrayList.size() != 0)
        {
            for (int i = 0; i < arrayList.size(); i++)
            {
                lblMSList.add(arrayList.get(i).get("lblMS"));
                lblASList.add(arrayList.get(i).get("lblES"));
                lblESList.add(arrayList.get(i).get("lblAS"));
            }
        }

Hope it helps.

3 Comments

Thanks a lot for replying Kishu. I will check and let you know
Can you explain what you doing. I didn't get. You are declaring a list lists for all again and adding to them.
I have fetch data from Hashmap and put it in the arraylist because we have multiple data for the same key.
1
    ArrayList<ArrayList<HashMap<String, String>>> li = new ArrayList<>();
    for (ArrayList<HashMap<String, String>> myList : li) {
        for (HashMap<String, String> myMap : myList) {
            for (String key : myMap.keySet()) {
                Log.e("key: ", "is " + key);
                Log.e("value: ", "is " + myMap.get(key));
            }
        }
    }

Please try it this way!

Comments

0

Check below Code:

List<ArrayList<HashMap<String, String>>> li = new ArrayList<ArrayList<HashMap<String, String>>>();

//Outer loop to get arraylist of list
for(int i = 0; i < li.size(); i++){

ArrayList<HashMap<String, String>> temp_list=li.get(i);

//inner loop to get hashMap for each entry inside arraylist 
for (Entry<String, String> mapEntry : temp_list.get(i).entrySet()) {
                    String key = mapEntry.getKey();
                    String value = mapEntry.getValue();
                    // Log.d("LIST:", "" + key + "::" + value);

                }


}

1 Comment

Thank you S.K. I will check and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.