-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?

2
  • 1
    loop through the list and store the email in String array Commented Oct 13, 2017 at 11:15
  • I would like to obtain the String[] directly if it is possible Commented Oct 13, 2017 at 11:17

1 Answer 1

2

Sure :

String[] result = clients
  .stream()
  .map(client->client.getEmail())
  .toArray(String[]::new)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clean and concise answer! I didn't know about the .toArray(String[]::new) trick.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.