I have an Iterator of Iterator, from which I want to generate a 2D array. I have the following code:
StreamSupport.stream(Spliterators.spliteratorUnknownSize(rowIterator, Spliterator.ORDERED), false)
.map((Row row) -> {
Iterator<Cell> cellIterator = row.cellIterator();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(cellIterator, Spliterator.ORDERED), false)
.map(Utils::genericCellMapper)
.toArray();
})
.toArray(Object[][]::new);
But Eclipse editor is giving compilation error: Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>).
If I change the above code as:
StreamSupport.stream(Spliterators.spliteratorUnknownSize(rowIterator, Spliterator.ORDERED), false)
.map(new Function<Row, Object[]>() {
@Override
public Object[] apply(Row row) {
Iterator<Cell> cellIterator = row.cellIterator();
List<Object> list = StreamSupport.stream(Spliterators.spliteratorUnknownSize(cellIterator, Spliterator.ORDERED), false)
.map(Utils::genericCellMapper)
.collect(Collectors.<Object>toList());
return list.toArray(new Object[list.size()]);
}
})
.toArray(Object[][]::new);
It works. What I understand that in the first variant compiler is unable to determine the return type of the mapper. Do I need to use the second variant to achieve the requirement or it is a problem with Eclipse?
Similar question from other SO threads
javacby any chance?ECJissuerowIterator. If it is a raw type, the behavior is not surprising. Besides that, you’re doing unnecessary work. There is no sense in collecting into aList, just to calltoArrayon it. UsetoArray()directly on the stream. Further, allIterables inherit adefault spliterator()method. So you should be able to useStreamSupport.stream(row.spliterator(), false).map(Utils::genericCellMapper).toArray(); I suppose, you could do similar for the outer stream, but you didn’t provide enough context.org.apache.poi.ss.usermodel.Sheet, you could simply doObject[][] array = StreamSupport.stream(sheet.spliterator(), false) .map(row -> StreamSupport.stream(row.spliterator(), false).map(Utils::genericCellMapper).toArray()) .toArray(Object[][]::new);…