为什么返回类型不正确

用户5596838

行 .flatMap(WordsUtil::getWords) 是错误 - 方法参考中的错误返回类型: cannot convert java.util.List<java.lang.String> to java.util.Iterator<U>

return lines.map(String::toLowerCase)
              .flatMap(WordsUtil::getWords)
              .mapToPair(w -> new Tuple2<>(w, 1))
              .reduceByKey((a, b) -> a + b)
              .mapToPair(Tuple2::swap)
              .sortByKey(false).map(Tuple2::_2).take(topX);

代码方法:

 public static List<String> getWords(String line) {
    List<String> words = new ArrayList<>();
    BreakIterator breakIterator = BreakIterator.getWordInstance();
    breakIterator.setText(line);
    int lastIndex = breakIterator.first();
    while (BreakIterator.DONE != lastIndex) {
        int firstIndex = lastIndex;
        lastIndex = breakIterator.next();
        if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(line.charAt(firstIndex))) {
            words.add(line.substring(firstIndex, lastIndex));
        }
    }

    return words;
}

为什么我有这个错误?

安迪·特纳

平面地图的函数需要返回一个迭代器:

.flatMap(w -> WordsUtil.getWords(w).iterator())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章