Iterable to ArrayList elements change

2019-04-16 03:17发布

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

3条回答
放荡不羁爱自由
2楼-- · 2019-04-16 03:54

Have you tried an iterator-based method?

Iterator i = values.iterator();
fileNames.add(i.next().getFileName());
for(i; i.hasNext();) {
    String stringI = i.next().getLast().getFileName();
    if(!stringI.equals(fileNames.get(fileNames.size() - 1)))
        fileNames.add(i.next().getLast().getFileName());
}
查看更多
男人必须洒脱
3楼-- · 2019-04-16 03:57

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()));
}
查看更多
贪生不怕死
4楼-- · 2019-04-16 04:04
for(String filename : filenames) {
  System.out.println(fn);
}

Let me know if this will help?

查看更多
登录 后发表回答