Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collect Objects in Array

Tags:

java

java-8

I want to filter the CC addresses of the array and want to collect it to the same array,

FOR EXAMPLE

String[] ccAddress = emails.split(";");
ccAddress = Arrays.stream(ccAddress)
                  .filter(adr -> "".equals(adr) == false)
                  .collect(Collectors.toArray);// ?????

My question is, 'Is there any direct way to collect filtered elements to the array in Java8' ?

NOTE : I know I can simply collect them to List and write list.toArray() to get array but that is not my question.

like image 478
akash Avatar asked Jun 23 '26 15:06

akash


1 Answers

Did you checked documentation?

You can use Stream.toArray method:

String[] ccAddress = emails.split(";");
ccAddress = Arrays.stream(ccAddress)
        .filter(addr -> !addr.isEmpty())
        .toArray(String[]::new);
like image 136
Dmitry Ginzburg Avatar answered Jun 25 '26 04:06

Dmitry Ginzburg