Reputation: 91
So i'm wondering if its possible to populate a String array made from toString() calls of an object ArrayList
So I know this can be done using a loop but is there a one line approach?
Loop approach
ArrayList<Object> objects = new ArrayList<Object>();
//fill object with elements
//
String[] strings = new String[object.length()];
for(int i = 0;i<10;i++)strings[i]=objects.get(i).toString();
Upvotes: 5
Views: 126
Reputation: 2523
Using java-8,
String[] strings = objects.stream().map(Object::toString).toArray(String[]::new);
Upvotes: 11