I have a boolean[]
and I want to turn into List<Integer>
. Each item in the list will be an index of the boolean[]
where that index must be true
. Right now I'm stuck with this code:
Stream.of(sieve)
.filter(x->x.equals(true))
.mapToInt(???)
.collect(toList());
Any idea on how to accomplish this (with streams of course)?
You have to introduce the indices to your Stream
pipeline by using IntStream::range
.
boolean[] sieve = sieve();
int[] numbers = IntStream.range(0, sieve.length).filter(i -> sieve[i]).toArray();
Or if you want a List<Integer>
as result:
List<Integer> = IntStream.range(0, sieve.length).filter(i -> sieve[i])
.boxed().collect(Collectors.toList());
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.