Iterable到ArrayList元素改变 [英] Iterable to ArrayList elements change
问题描述
我有变量
Iterable< FreqDataWritable>值
FreqDataWritable是一个包含多条信息的对象,但现在我只关注一块它包含的信息是通过getFilename()访问的字符串。
我有以下循环:
的ArrayList<字符串> filenames = new ArrayList< String>(); (FreqDataWritable i:values)
{
filenames.add(i.getFilename());
$ b现在我想要做的就是打印数组列表文件名中的值。 for(int i = 0; i< filenames.size(); i ++){
System.out .println(filenames.get(I));
}
然而,当我这样做时,文件名中的所有内容都是相同的。打印出来的唯一情况是多次打印一个文件名。
我的原始代码比这个更复杂,但我简化了它的帮助。有人知道怎么修这个东西吗?
谢谢
解决方案我想通了。 Hadoop有一个奇怪的内存使用情况,所以当我第一次迭代这些值时,它只是一次又一次地将相同的对象添加到数组列表中。
相反,我需要这样做:
$ b $ pre $ for(FreqDataWritable i:values){
filenames.add(new String(i.getFilename( )));
}
So I am implementing a mapreduce job which means I am dealing with key value pairs.
I have the variable
Iterable<FreqDataWritable> values
FreqDataWritable is an object that contains pieces of information, but for now I am only concerned with one piece of information it holds which is a String which is accessed by getFilename().
I have the following loop:
ArrayList<String> filenames = new ArrayList<String>();
for(FreqDataWritable i : values) {
filenames.add(i.getFilename());
}
Now all I want to do is print the values in the array list filenames.
for(int i = 0; i < filenames.size(); i++) {
System.out.println(filenames.get(i));
}
However when I do this everything in filenames is the same. The only thing printed out is a single filename printed multiple times.
My original code is more complex than this, but I simplified it for help. Anyone know how to fix this?
Thanks
I figured it out. Hadoop has an odd memory usage so when I iterated over the values the first time it was just adding the same object over and over again to the arraylist.
Instead I need to do this:
for(FreqDataWritable i : values) {
filenames.add(new String(i.getFilename()));
}
这篇关于Iterable到ArrayList元素改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!