title: Custom hadoop key and value : How to write CompareTo() Method
body: <p>I need to emit a 2D double array as key and value from mapper. There are questions posted in Stack Overflow, but they are not answered.</p>
  
  <p>I am doing some of the matrix multiplication in a given dataset, and after that I need to emit the value of <code>A*Atrns</code> which will be a matrix as key and <code>Atrans*D</code> which will also be a matrix as value. So how to emit these matrices  from mapper. And the value should be corresponding to the key itself.</p>
  
  <pre><code>ie key -----&gt;  A*Atrans---------&gt;after multiplication the result will be a 2D array which is declared as double (matrix) lets say the result be Matrix "Ekey"(double[][] Ekey)
  
  value ------&gt;  Atrans*D ---------&gt; after multiplication the result will be Matrix "Eval" (double[][] Eval).
  
  After that I need to emit these matrix to reducer for further calculations.
  
  So in mapper: 
         context.write(Ekey,Eval);
  
  Reducer:
        I need to do further calculations with these Ekey and Eval.
  </code></pre>
  
  <p>I wrote my class: </p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>    public class MatrixWritable implements WritableComparable&lt;MatrixWritable&gt;{
  
  /**
   * @param args
   */
      private double[][] value;
      private double[][] values;
      public MatrixWritable() {
      // TODO Auto-generated constructor stub
  
          setValue(new double[0][0]);
       }
  
  
      public MatrixWritable(double[][] value) {
      // TODO Auto-generated constructor stub
  
       this.value = value;
      }
  
      public void setValue(double[][] value) {
  
          this.value = value;
  
      }
  
      public double[][] getValue() {
          return values;
      }
  
      @Override
      public void write(DataOutput out) throws IOException {
      out.writeInt(value.length);                 // write values
       for (int i = 0; i &lt; value.length; i++) {
         out.writeInt(value[i].length);
       }
       for (int i = 0; i &lt; value.length; i++) {
         for (int j = 0; j &lt; value[i].length; j++) {
             out.writeDouble(value[i][j]);
         }
       }
  
    }
  
      @Override
      public void readFields(DataInput in) throws IOException {
  
          value = new double[in.readInt()][];          
          for (int i = 0; i &lt; value.length; i++) {
            value[i] = new double[in.readInt()];
          }
          values = new double[value.length][value[0].length];
        for(int i=0;i&lt;value.length ; i++){
              for(int j= 0 ; j&lt; value[0].length;j++){
                  values[i][j] = in.readDouble();
  
              }
          }
  
    }
  
  
  
  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + Arrays.hashCode(value);
      return result;
  }
  
  
  
  
  
  /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
      if (this == obj) {
          return true;
      }
      if (obj == null) {
          return false;
      }
      if (!(obj instanceof MatrixWritable)) {
          return false;
      }
      MatrixWritable other = (MatrixWritable) obj;
      if (!Arrays.deepEquals(value, other.value)) {
          return false;
      }
      return true;
  }
  
  
      @Override
      public int compareTo(MatrixWritable o) {
      // TODO Auto-generated method stub
      return 0;
  
  
      }
  
      public String toString() { String separator = "|";
          StringBuffer result = new StringBuffer();
  
          // iterate over the first dimension
          for (int i = 0; i &lt; values.length; i++) {
              // iterate over the second dimension
              for(int j = 0; j &lt; values[i].length; j++){
                  result.append(values[i][j]);
  
                  result.append(separator);
              }
              // remove the last separator
              result.setLength(result.length() - separator.length());
              // add a line break.
              result.append(",");
          }
  
  
          return result.toString();
  
  
  
    }
  
  }
  </code></pre>
  
  <p>I am able to emit a value as matrix from mapper</p>
  
  <pre><code>context.write(...,new MatrixWritable(AAtrans));
  </code></pre>
  
  <p>How to emit matrix AtransD as key from mapper?</p>
  
  <p>For that I need to write compareto() method, right?</p>
  
  <p>What should be included in that method? </p>