我有一个map reduce程序,它将输入作为firstname,lastname和mobile number。我想将这3个字段组合为一个单独的键。为此,我使用WritableComparable
接口作为类。
我的代码是:
private static class Multifield implements WritableComparable<Multifield> {
Text firstname1;
Text lastname1;
Text mobile1;
public Multifield(Text firstname1, Text lastname1,Text mobile1) {
this.firstname1 = firstname1;
this.lastname1 = lastname1;
this.mobile1=mobile1;
}
public Multifield() {
this.firstname1 = new Text();
this.lastname1 = new Text();
this.mobile1=new Text();
}
public void write(DataOutput out) throws IOException {
this.firstname1.write(out);
this.lastname1.write(out);
this.mobile1.write(out);
}
public void readFields(DataInput in) throws IOException {
this.firstname1.readFields(in);
this.lastname1.readFields(in);
this.mobile1.readFields(in);
}
public int compareTo(Multifield pop) {
if(firstname1.equals(pop.firstname1))
{
return lastname1.compareTo(pop.lastname1);
}
else
{
return firstname1.compareTo(pop.firstname1);
}
}
@Override
public String toString() {
return "Customer Details [firstname=" + firstname1 + ", lastname=" + lastname1
+ ", mobile=" + mobile1 + "]";
}
上面的编码只比较了firstname和lastname。 所以,我只根据前两个字段得到输出。
如何修改compareTo()方法来比较所有三个字段,以便我可以将它们组合在一起作为键?
答案 0 :(得分:2)
类似于this one的一个不错的解决方案如下:
public int compareTo(Multifield pop) {
int i = firstname1.compareTo(pop.firstname1);
if (i != 0) return i; //stop here if first names are not equal, and return their difference
i = lastname1.compareTo(pop.lastname1);
if (i != 0) return i; //stop here if last names are not equal, and return their difference.
return mobile1.compareTo(pop.mobile1); //at this point, first names and last names are equal, so return the difference of the mobiles
}
答案 1 :(得分:1)
我们用于比较2个字段的逻辑是:
if(field1 = field2) then Result = 0;
if(field1 > field2) then Result = 1;
if(field1 < field2) then Result = -1;
为了便于解释,我们假设我们正在比较两个相同类型的对象,包含字段a,b和c。
Object 1 contains a1, b1 and c1 (instances of a, b and c)
Object 2 contains a2, b2 and c2 (instances of a, b and c)
以下是不同的情况,我们需要处理:
a) if(a1 = a2, b1 = b2, c1 = c2) then Result = 0; // (All equal)
b) if(a1 > a2) then Result = 1; // (Since a1 > a2, we short circuit at a)
c) if(a1 < a2) then Result = -1; // (Since a1 < a2, we short circuit at a)
d) if(a1 = a2, b1 > b2) then Result = 1; // (Since b1 > b2, we short circuit at b)
e) if(a1 = a2, b1 < b2) then Result = -1; // (Since b1 < b2, we short circuit at b)
f) if(a1 = a2, b1 = b2, c1 > c2) then Result = 1; // (Since c1 > c2)
g) if(a1 = a2, b1 = b2, c1 < c2) then Result = -1; // (Since c1 < c2)
这个代码是:
public int compareTo(Person otherPerson) {
int cmp = firstName.compareTo(otherPerson.firstName);
if(cmp != 0) return cmp;
cmp = lastName.compareTo(otherPerson.lastName);
if(cmp != 0) return cmp;
return mobile.compareTo(otherPerson.mobile);
}