为什么返回类型不正确

时间:2018-10-25 19:32:00

标签: java apache-spark rdd flatmap

line .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;
}

为什么我有这个错误?

1 个答案:

答案 0 :(得分:0)

平面图的函数需要返回一个Iterator:

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