我想用此代码将Text distinctWords[]
转换为List<String>
:
List<String> asList = Arrays.asList(distinctWords);
但是它给出了一个错误
Hadoop - Type mismatch: cannot convert from List<Text> to List<String>.
如何将List<Text>
转换为List<String>
?
因为Text
不是String
,所以无法进行直接转换。但是,这可以通过简单的范围来完成:
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());