简体   繁体   中英

Convert from Arrays.asList to Array

I have this code as below :

fieldsToFilter.stream()
    .map(e -> Arrays.asList(
        Filters.ne(e, ""),
        Filters.exists(e, true)
     ))
     .toArray(Bson[]::new))

while executing I get java.lang.ArrayStoreException: java.util.Arrays$ArrayList

Filters.exists and Filters.ne return new Bson instances. See the docs .

Any help is appreciated

You can use flatMap instead of map and

Stream.of instead of Arrays.asList

Bson[] result = fieldsToFilter.stream()
                   .flatMap(e -> Stream.of(Filters.ne(e, "") , Filters.exists(e, true)))
                   .toArray(Bson[]::new);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM