-1

I have a List of 'Client' objects each one with a field "email".

I need something like:

List<String> listEmails = clients.stream().map(client->client.getEmail())
                                               .collect(Collectors.toList());

...but returning directly a String[].

Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?

Naman
  • 27,789
  • 26
  • 218
  • 353
DavidPi
  • 415
  • 3
  • 18

1 Answers1

1

Sure :

String[] result = clients
  .stream()
  .map(client->client.getEmail())
  .toArray(String[]::new)
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • Thanks for the clean and concise answer! I didn't know about the .toArray(String[]::new) trick. – DavidPi Oct 13 '17 at 11:32