Transform List into Map using only two keys and odd or even list indexes as values - Java 8 Stream
You were on the right track with streaming over the indexes:
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
IntStream.range(0,list.size())
.boxed()
.collect(groupingBy(
i -> i % 2 == 0 ? "even" : "odd",
mapping(list::get, toList())
));
If you are ok with having your map be indexed by a boolean
you can use partitioningBy
:
IntStream.range(0, list.size())
.boxed()
.collect(partitioningBy(
i -> i % 2 == 0,
mapping(list::get, toList())
));