Collecting objects in an array
I want to filter the addresses of an CC
array and want to collect it into 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 items into an array in Java8"?
NOTE. I know I can just collect them into a List and write list.toArray()
to get an array, but that's not my question.
+3
source to share
1 answer
Have you checked the documentation ?
You can use the method Stream.toArray
:
String[] ccAddress = emails.split(";");
ccAddress = Arrays.stream(ccAddress)
.filter(addr -> !addr.isEmpty())
.toArray(String[]::new);
+9
source to share