title: In Hadoop, if you want to keep the values from each key-value pair into an Array, why are all the elements that you add the same?
body: <p>I'm trying to store the values from the key-value pairs that the Map function gets and use them further. Given the following input:</p>
  
  <pre><code>Hello hadoop goodbye hadoop
  Hello world goodbye world
  Hello thinker goodbye thinker
  </code></pre>
  
  <p>An the following code:</p>
  
  <p><strong>Note</strong> - the map is the simple WordCount example</p>
  
  <pre><code>public class Inception extends Configured implements Tool{
  
  public Path workingPath;
  
   public static class Map extends Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {
      private final static IntWritable one = new IntWritable(1);
      private Text word = new Text();
  
       // initialising the arrays that contain the values and the keys
      public ArrayList&lt;LongWritable&gt; keyBuff = new ArrayList&lt;LongWritable&gt;();
      public ArrayList&lt;Text&gt; valueBuff = new ArrayList&lt;Text&gt;();
  
  
      public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
          String line = value.toString();
          StringTokenizer tokenizer = new StringTokenizer(line);
  
          while (tokenizer.hasMoreTokens()) {
              word.set(tokenizer.nextToken());
              context.write(word, one);
              System.out.println(word + " / " + one);
          }
      }   
  
      public void innerMap(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  
              // adding the value to the bufferr
          valueBuff.add(value);
          System.out.println("ArrayList addValue -&gt; " + value);
          for (Text v : valueBuff){
              System.out.println("ArrayList containedValue -&gt; " + value);
          }
  
          keyBuff.add(key);
  
          }   
  
      public void run(Context context) throws IOException, InterruptedException {
          setup(context);
  
          // going over the key-value pairs and storing them into the arrays
          while(context.nextKeyValue()){
              innerMap(context.getCurrentKey(), context.getCurrentValue(), context);
          }
  
  
          Iterator itrv = valueBuff.iterator();
          Iterator itrk = keyBuff.iterator();
          while(itrv.hasNext()){
              LongWritable nextk = (LongWritable) itrk.next();
              Text nextv = (Text) itrv.next();
              System.out.println("Value iterator -&gt; " + nextv);
              System.out.println("Key iterator -&gt; " + nextk);
  
              // iterating over the values and running the map on them.
  
              map(nextk, nextv, context);
          }
  
          cleanup(context);
      }
   }
  
   public int run(String[] args) throws Exception { ... }
  
   public static void main (..) { ... }
  </code></pre>
  
  <p>Ok, now the log output:</p>
  
  <p><strong>stdout logs</strong></p>
  
  <pre><code>ArrayList addValue -&gt; Hello hadoop goodbye hadoop
  ArrayList containedValue -&gt; Hello hadoop goodbye hadoop
  ArrayList addValue -&gt; Hello world goodbye world
  ArrayList containedValue -&gt; Hello world goodbye world
  ArrayList containedValue -&gt; Hello world goodbye world
  ArrayList addValue -&gt; Hello thinker goodbye thinker
  ArrayList containedValue -&gt; Hello thinker goodbye thinker
  ArrayList containedValue -&gt; Hello thinker goodbye thinker
  ArrayList containedValue -&gt; Hello thinker goodbye thinker
  Value iterator -&gt; Hello thinker goodbye thinker
  Key iterator -&gt; 84
  Hello / 1
  thinker / 1
  goodbye / 1
  thinker / 1
  Value iterator -&gt; Hello thinker goodbye thinker
  Key iterator -&gt; 84
  Hello / 1
  thinker / 1
  goodbye / 1
  thinker / 1
  Value iterator -&gt; Hello thinker goodbye thinker
  Key iterator -&gt; 84
  Hello / 1
  thinker / 1
  goodbye / 1
  thinker / 1
  </code></pre>
  
  <p>So what you can notice is, that every time i add a new value to the ArrayList valueBuff, all the values in the list gets overwritten. Does anybody know why this is going on an why the values are not added properly in the Array?</p>