我可以在“foreach”中使用 iterable 一次又一次地遍历列表吗?
我可以使用 iterable 一次又一次地遍历列表吗? 当我使用ArrayList时,我可以在“foreach”中一次又一次地遍历列表,但是当我在hadoop(分布式计算框架)中的函数中使用iterable作为参数时,只有第一次我可以遍历iterable,当我再次使用 foreach ,它什么也得不到。 ex:
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
float all=0;
String resultKey;
float resultValue;
ArrayList<String> valuelist=new ArrayList<String>();
for (Text text : values) {
valuelist.add(text.toString());
}
for (String text : valuelist) {
String[] contents=text.toString().split(" ");
if(contents.length==1)
{
all=Float.parseFloat(contents[0]);
break;
}
}
if(all==0)
{
return;
}
for (String text : valuelist) {
String[] contents=text.toString().split(" ");
if(contents.length>1)
{
resultKey=contents[0]+" "+key.toString();
resultValue=Float.parseFloat(contents[1])/all;
context.write(new Text(resultKey), new Text(resultValue+""));
}
}
}
-----我必须先将它保存在 ArrayList 中... 在我的理解中,foreach只需要一个iterable,为什么ArrayList可以,但参数不能? 感谢您阅读了这么多。
can i use iterable traverse the list again and again?
when i use ArrayList,i can traverse the list again and again in "foreach" ,but when i use iterable as a parameter in a function in hadoop(a distributed compute framework),only the first time i can traverse the iterable, when i use foreach again , it get nonthing.
ex:
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
float all=0;
String resultKey;
float resultValue;
ArrayList<String> valuelist=new ArrayList<String>();
for (Text text : values) {
valuelist.add(text.toString());
}
for (String text : valuelist) {
String[] contents=text.toString().split(" ");
if(contents.length==1)
{
all=Float.parseFloat(contents[0]);
break;
}
}
if(all==0)
{
return;
}
for (String text : valuelist) {
String[] contents=text.toString().split(" ");
if(contents.length>1)
{
resultKey=contents[0]+" "+key.toString();
resultValue=Float.parseFloat(contents[1])/all;
context.write(new Text(resultKey), new Text(resultValue+""));
}
}
}
-----i have to save it in the ArrayList first...
in my understanding, foreach only need an iterable, why ArrayList can,but the parameter can't?
thanks for reading so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,这不是 Iterable 的问题,但也许是框架传递给该方法的特定实现类的问题。如果您考虑一下,当您调用 next(); 时,该类可能会通过网络拉取 Iterable 的每个元素;这可以解释为什么你不能再次运行它。如果这确实是您需要做的,那么保存每个元素是一个很好的解决方案。
This isn't a problem with Iterable in general, but perhaps it's a problem with the specific implementation class that's being passed to that method by the framework. If you think about it, that class is probably pulling each element of the Iterable over the network right when you call next(); that would explain why you can't just run it over again. Saving each element is a good solution if that's really what you need to do.
我认为这个问题与迭代器是一种单向、一次性的集合有关。来自谷歌收藏常见问题解答:
I think the issue has to do with iterators being one-way, one-time kinds of collections. From the google-collections faq: