[英]Getting compilation error “Can only iterate over an array or an instance of java.lang.Iterable” at for loop in reducer
Getting compilation error "Can only iterate over an array or an instance of java.lang.Iterable" at for loop in reducer. 在reducer中的for循环中获取编译错误“只能迭代数组或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();
In above code, getting compilation error "Can only iterate over an array or an instance of java.lang.Iterable" at "for (IntWritable val : values)"
. 在上面的代码中,在
"for (IntWritable val : values)"
处出现编译错误“只能迭代数组或java.lang.Iterable实例"for (IntWritable val : values)"
。 How to use loop in the same? 如何在相同的地方使用循环?
Try this. 尝试这个。
for (IntWritable val : (Iterable<IntWritable>)() -> values) {
sum += val.get();
....
(Iterable<IntWritable>)() -> values
is equivalent to (Iterable<IntWritable>)() -> values
等效于
new Iterable<IntWritable>() {
@Override
public Iterator<IntWritable> iterator() {
return values;
}
};
Either pass an Iterable<IntWritable>
instead of Iterator<IntWritable>
to the method, or use hasNext()
and next()
to iterate over the elements of the Iterator
: 将
Iterable<IntWritable>
而不是Iterator<IntWritable>
传递给方法,或者使用hasNext()
和next()
遍历Iterator
的元素:
while (values.hasNext()) {
IntWritable val = values.next();
...
}
Iterator generally points to a single instance in a collection. 迭代器通常指向集合中的单个实例。 So
for (IntWritable val : values)
is not valid. 因此
for (IntWritable val : values)
是无效的。
Use Iterable
instead. 改用
Iterable
。 Pass Iterable<IntWritable>
in your function. 在您的函数中传递
Iterable<IntWritable>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.