如何在Java中使用Stream of Stream创建2D(二维)数组?

时间:2018-04-25 13:12:23

标签: java eclipse java-8 java-stream

我有一个Iterator Iterator,我想从中生成一个二维数组。我有以下代码:

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);

但Eclipse编辑器正在编译错误:Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)

如果我将上述代码更改为:

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);

有效。据我所知,在第一个变种编译器中无法确定映射器的返回类型。我是否需要使用第二个变体来满足要求,或者它是Eclipse的问题?

来自其他SO主题的类似问题

0 个答案:

没有答案