可迭代到 ArrayList 元素更改 [英] Iterable to ArrayList elements change
问题描述
所以我正在实施一个 mapreduce 工作,这意味着我正在处理键值对.
So I am implementing a mapreduce job which means I am dealing with key value pairs.
我有变量
Iterable<FreqDataWritable> values
FreqDataWritable 是一个包含信息片段的对象,但现在我只关心它保存的一条信息,它是一个由 getFilename() 访问的字符串.
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().
我有以下循环:
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?
谢谢
推荐答案
我想通了.Hadoop 的内存使用量很奇怪,所以当我第一次迭代这些值时,它只是一遍又一遍地将相同的对象添加到数组列表中.
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.
相反,我需要这样做:
for(FreqDataWritable i : values) {
filenames.add(new String(i.getFilename()));
}
这篇关于可迭代到 ArrayList 元素更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!