-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
  • 21,685
  • 24
  • 196
  • 332
DavidPi
  • 385
  • 2
  • 17

1 Answers1

1

Sure :

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