ArrayList<HashMap<String,String>> to String[]

35,269

Solution 1

try

ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
HashMap<String, String> n = new HashMap<String, String>();
n.put("a", "a");
n.put("b", "b");
test.add(n);

HashMap<String, String> m = test.get(0);//it will get the first HashMap Stored in array list 

String strArr[] = new String[m.size()];
int i = 0;
for (HashMap<String, String> hash : test) {
    for (String current : hash.values()) {
        strArr[i] = current;
        i++;
    }
}

Solution 2

The uses for an Hashmap should be an Index of HashValues for finding the values much faster. I don't know why you have Key and Values as Strings but if you only need the values you can do it like that:

ArrayList<HashMap<String, String>> test = new ArrayList<>();
String sum = "";
for (HashMap<String, String> hash : test) {
    for (String current : hash.values()) {
        sum = sum + current + "<#>";
    }
}
String[] arr = sum.split("<#>");

It's not a nice way but the request isn't it too ;)

Share:
35,269
Niraj Adhikari
Author by

Niraj Adhikari

I am an Xamarin/iOS/Java/Android Developer / System &amp; Network Administrator from Kathmandu Nepal.

Updated on March 03, 2020

Comments

  • Niraj Adhikari
    Niraj Adhikari about 4 years

    i have data fetched from my webservice in

    ArrayList<HashMap<String,String>>
    

    Now i want to convert each object of the above to

    String[]
    

    how do i do this? any help would be much appreciated!