5

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!

Raghunandan
  • 131,557
  • 25
  • 223
  • 252
Niraj Adhikari
  • 1,618
  • 2
  • 13
  • 28
  • it contains data obtained from a webservice serving json data. i need to extract each field as a string array for populating them into different views like viwepagers and list views. – Niraj Adhikari Jul 29 '13 at 11:33
  • 1
    http://stackoverflow.com/questions/1090556/java-how-to-convert-hashmapstring-object-to-array.check this might help. – Raghunandan Jul 29 '13 at 12:00
  • In the array you want to put hashmap's keys, values or both? – Alberto Jul 29 '13 at 12:58
  • 1
    Please cover my tutorial on internal life of HashMap,ArrayList and other Java collection types [here](http://volodial.blogspot.com/search/label/Java%20Collections) – Volodymyr Levytskyi Jul 29 '13 at 13:52

3 Answers3

8

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++;
    }
}
JeroenWarmerdam
  • 402
  • 2
  • 9
Tarsem Singh
  • 13,979
  • 7
  • 50
  • 70
1

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 ;)

Chandresh
  • 628
  • 2
  • 13
  • 30
JavaDM
  • 851
  • 1
  • 5
  • 28
0
 ArrayList<HashMap<String, String>> meterList = controller.getMeter();

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

String[] strMeter = new String[mtiti.size()];

String meter = "";
for (HashMap<String, String> hash : meterList) {
    for (String current : hash.values()) {
        meter = meter + current + "<#>";
    }
}
String[] arr = meter.split("<#>");
Chandresh
  • 628
  • 2
  • 13
  • 30