Question

public void operation(Text key, Iterable<HashMapWritable<Text, ArrayListOfIntsWritable>> values, Context context) throws IOException, InterruptedException
{
    Set<HashMapWritable<Text, ArrayListOfIntsWritable>> valueSet = new HashSet<HashMapWritable<Text, ArrayListOfIntsWritable>>();
    for (HashMapWritable<Text, ArrayListOfIntsWritable> value : values) {
        valueSet.add(value);
        System.out.println(value);
    }
    System.out.println("break 1");
    System.out.println(valueSet);
    System.out.println("break 2");

So my operation takes in this iterable, and adds it to a hashset, however if the size of the iterator is N, the valueSet contains N times the final value input. The system println's give the following results as an example:

 [exec] {Bart_the_Fink.txt.gz=[25]}
 [exec] {Bart_the_General.txt.gz=[28]}
 [exec] {Bart_the_Lover.txt.gz=[25]}
 [exec] {Bart_the_Murderer.txt.gz=[25]}
 [exec] {Bart_the_Mother.txt.gz=[23]}
 [exec] {Bart_the_Genius.txt.gz=[28]}
 [exec] break 1
 [exec] [{Bart_the_Genius.txt.gz=[28]}, {Bart_the_Genius.txt.gz=[28]}, {Bart_the_Genius.txt.gz=[28]}, {Bart_the_Genius.txt.gz=[28]}, {Bart_the_Genius.txt.gz=[28]}, {Bart_the_Genius.txt.gz=[28]}]
 [exec] break 2

For the life of me I can't figure out why

Was it helpful?

Solution

Some iterators actually return the same mutable object each time, set to different values internally. The way to copy such a collection is to copy each element (not such a copy of the reference to the element) before adding it to the new set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top