Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 474
Niraj Adhikari Avatar asked Jul 29 '13 11:07

Niraj Adhikari


People also ask

What is the difference between String [] and list string in Java?

An array String[] cannot expand its size. You can initialize it once giving it a permanent size: String[] myStringArray = new String[20](); myStringArray[0] = "Test"; An ArrayList<String> is variable in size.

Can you put an ArrayList in a HashMap?

Array List can be converted into HashMap, but the HashMap does not maintain the order of ArrayList. To maintain the order, we can use LinkedHashMap which is the implementation of HashMap.

Can we convert ArrayList to string?

To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method.


2 Answers

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++;
    }
}
like image 182
Tarsem Singh Avatar answered Sep 28 '22 02:09

Tarsem Singh


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

like image 28
JavaDM Avatar answered Sep 28 '22 03:09

JavaDM