您可以使用一招。
诀窍是通过键将减速器以这样的方式即,上述(a,d)和(d,α)具有相同的密钥,并在同一减速结束:
当(一,d)来自:
'a' < 'd', hence emit:
key => a,d
value => a,d
当(d,α)来了:总是形成
'd' > 'a', hence emit:
key => a,d
value => d,a
键以这样的方式,较低的字母而来的较高的字母之前。所以对于这两种记录,关键是“一,d”
所以映射器的输出将是:
Record: a,b
Key = a,b Value = a,b
Record: a,c
Key = a,c Value = a,c
Record: a,d
Key = a,d Value = a,d
Record: c,b
Key = b,c Value = c,b
Record: b,d
Key = b,d Value = b,d
Record: d,a
Key = a,d Value = d,a
Record: b,c
Key = b,c Value = b,c
Record: b,e
Key = b,e Value = b,e
Record: e,f
Key = e,f Value = e,f
现在,在减速中的记录将按照以下顺序到达:
Record 1:
Key = a,b Value = a,b
Record 2:
Key = a,c Value = a,c
Record 3:
Key = a,d Value = a,d
Key = a,d Value = d,a
Record 4:
Key = b,c Value = c,b
Key = b,c Value = b,c
Record 5:
Key = b,d Value = b,d
Record 6:
Key = b,e Value = b,e
Record 7:
Key = e,f Value = e,f
因此,在减速机,你可以解析记录3和4:
Record 3:
Key = a,d Value = a,d
Key = a,d Value = d,a
Record 4:
Key = b,c Value = c,b
Key = b,c Value = b,c
因此,输出将是:
a,d
c,b
这个逻辑会的工作,即使你有名字的,而不是字母。 例如你需要使用下面的逻辑在映射器侧(其中S1是第一个字符串和s2是第二个字符串):
String key = "";
int compare = s1.compareToIgnoreCase(s2);
if(compare >= 0)
key = s1 + "," + s2;
else
key = s2 + "," + s1;
所以,如果您有:
String s1 = "Stack";
String s2 = "Overflow";
的关键是:
Stack,Overflow
同样,如果您有:
s1 = "Overflow";
s2 = "Stack";
仍然,关键将是:
Stack,Overflow
大@Manjunath。最佳答案.. – Thanga
我如何实现它,如果我有一些名称,而不是字母? @manjunath –
它会工作相同。只需要根据字母命令名称。 –