WritableComparable interface is just a subinterface of the Writable and java.lang.Comparable interfaces. For implementing a WritableComparable we must have compareTo method apart from readFields and write methods, as shown below:
public interface WritableComparable extends Writable, ComparableComparison of types is crucial for MapReduce, where there is a sorting phase during which keys are compared with one another.
void readFields(DataInput in);
void write(DataOutput out);
int compareTo(WritableComparable o)
The code for IntPair class which is used in In-mapper Combiner Program to Calculate Average post is given below:
As you can see in compareTo(IntPair tp) of above class that IntPair needs to be deserialized for comparison to take place, we can implement a RawComparator which can compare two keys by just checking their serialized representation. More on RawComparator is available in Hadoop: The Definitive Guide.