user3370533
user3370533

Reputation: 91

How to get a String[] consisting of the .toString() calls of an ArrayList<Object> in one line

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

Answers (1)

bradimus
bradimus

Reputation: 2523

Using java-8,

String[] strings = objects.stream().map(Object::toString).toArray(String[]::new);

Upvotes: 11

Related Questions