在 reducer 的 for 循环中出现编译错误“Can only iterate over an array or an instance of java.lang.Iterable”。
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> Output, Reporter arg3)
throws IOException {
// TODO Auto-generated method stub
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
在上面的代码中,在 “for (IntWritable val : values)”
处出现编译错误“Can only iterate over an array or an instance of java.lang.Iterable”。如何使用循环?
最佳答案
或者传递一个 Iterable<IntWritable>
而不是 Iterator<IntWritable>
方法,或使用 hasNext()
和 next()
遍历 Iterator
的元素:
while (values.hasNext()) {
IntWritable val = values.next();
...
}
关于java - 在 reducer 的 for 循环中获取编译错误 "Can only iterate over an array or an instance of java.lang.Iterable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36078389/