Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
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  
stackoverflow.com/questions/1090556/… 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 –  Volodymyr Levytskyi Jul 29 '13 at 13:52

2 Answers 2

up vote 0 down vote accepted

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++;
    }
}
share|improve this answer

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|improve this answer
    
thank you Its working :) –  Subhalaxmi Nayak Jan 13 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.