【问题标题】:Hadoop - Type mismatch: cannot convert from List<Text> to List<String>Hadoop - 类型不匹配:无法从 List<Text> 转换为 List<String>
【发布时间】:2016-12-15 15:49:43
【问题描述】:

我想使用以下代码将Text distinctWords[] 转换为List&lt;String&gt;

List<String> asList = Arrays.asList(distinctWords);

但它给出了一个错误

Hadoop - Type mismatch: cannot convert from List<Text> to List<String>. 

如何将List&lt;Text&gt; 转换为List&lt;String&gt;

【问题讨论】:

    标签: java arrays list hadoop hashset


    【解决方案1】:

    因为Text不是String,所以不能直接转换。但是,这可以通过简单的 for-each 来完成:

    List<String> strings = new ArrayList<> ();
    for (Text text : distinctWords) {
        strings.add(text.toString());
    }
    

    或者使用 Java 8 流

    List<String> strings = Arrays.stream(distinctWords)
        .map(word -> word.toString())
        .collect(Collectors.toList());
    

    【讨论】:

    • 非常感谢先生的回答。我正在尝试那个解决方案。
    猜你喜欢
    • 2016-04-08
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多