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
134k26 gold badges232 silver badges261 bronze badges
asked Jul 29, 2013 at 11:26
-
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– Niraj Adhikari2013年07月29日 11:33:32 +00:00Commented Jul 29, 2013 at 11:33
-
1stackoverflow.com/questions/1090556/… this might help.Raghunandan– Raghunandan2013年07月29日 12:00:55 +00:00Commented Jul 29, 2013 at 12:00
-
In the array you want to put hashmap's keys, values or both?Alberto– Alberto2013年07月29日 12:58:31 +00:00Commented Jul 29, 2013 at 12:58
-
1Please cover my tutorial on internal life of HashMap,ArrayList and other Java collection types hereVolodymyr Levytskyi– Volodymyr Levytskyi2013年07月29日 13:52:33 +00:00Commented Jul 29, 2013 at 13:52
3 Answers 3
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
4102 silver badges9 bronze badges
answered Jul 29, 2013 at 11:59
Sign up to request clarification or add additional context in comments.
Comments
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 Kachariya
6772 gold badges14 silver badges33 bronze badges
answered Jul 29, 2013 at 11:37
Comments
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 Kachariya
6772 gold badges14 silver badges33 bronze badges
Comments
lang-java