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
134k26 gold badges232 silver badges261 bronze badges
asked Jul 29, 2013 at 11:26
4
  • 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. Commented Jul 29, 2013 at 11:33
  • 1
    stackoverflow.com/questions/1090556/… this might help. Commented Jul 29, 2013 at 12:00
  • In the array you want to put hashmap's keys, values or both? Commented Jul 29, 2013 at 12:58
  • 1
    Please cover my tutorial on internal life of HashMap,ArrayList and other Java collection types here Commented Jul 29, 2013 at 13:52

3 Answers 3

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++;
 }
}
answered Jul 29, 2013 at 11:59
Sign up to request clarification or add additional context in comments.

Comments

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 Kachariya
6772 gold badges14 silver badges33 bronze badges
answered Jul 29, 2013 at 11:37

Comments

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 Kachariya
6772 gold badges14 silver badges33 bronze badges
answered Jan 27, 2016 at 8:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.