我有两个地图,Map<String,Object>
,看起来像:
Map<String, Object> map1 = {
taxRate={rateType=EXCLUSIVE, rate=0.07},
taxAmount={currencyCode=USD, amount=7.0},
jurisdictionTaxes=[{jurisdictionLevel=State, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=County, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=City, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=District, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}],
withholdingJurisdictionTaxes=[]
}
Map<String, Object> map2 = {
taxRate={rateType=EXCLUSIVE, rate=0.07},
taxAmount={currencyCode=USD, amount=7.0},
jurisdictionTaxes=[{jurisdictionLevel=State, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=County, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=City, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}, {jurisdictionLevel=District, taxAmount={currencyCode=USD, amount=null}, taxRate={rateType=EXCLUSIVE, rate=null}, displayName=, jurisdictionName=, responsiblePartyReason=}],
withholdingJurisdictionTaxes=[]
}
我用
比较这两个地图Map<String, Object> flatMap1 = FlatMapUtil.flatten(map1);
Map<String, Object> flatMap2 = FlatMapUtil.flatten(map2);
MapDifference<String, Object> difference = Maps.difference(flatMap1, flatMap2);
但是,如果您查看映射中的第二个键值,它是一个对象列表。在计算diff时,顺序很重要,所以我需要在找到Map.difference之前对特定列表进行排序。
你能告诉我如何排序这个列表吗?
我试了如下:
1. Collections.sort(Arrays.asList(leftMap.get("jurisdictionTaxes")));
Error: Required type: List <T>
Provided: List <Object>
2. Arrays.asList(leftMap.get("jurisdictionTaxes")).sort((s1, s2) -> s1.get("jurisdictionLevel") - s2.get("jurisdictionLevel"));
Error: Cannot resolve method 'get' in 'Object'
public static <K, V> MapDifference<K, V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
if (left instanceof SortedMap) {
SortedMap<K, ? extends V> sortedLeft = (SortedMap)left;
return difference(sortedLeft, right);
} else {
return difference(left, right, Equivalence.equals());
}
}
我认为这是关键:
Error: Required type: List <T>
排序需要一个可比较的类来排序。试着对管辖权进行排序:
Arrays.sort((JurisdictionTax[]) map1.get("jurisdictionTaxes"));
,
static Map<String, Object> map1 = new HashMap<>();
static {
map1.put("taxRate", "rateType=EXCLUSIVE, rate=0.07");
map1.put("taxAmount", "currencyCode=USD, amount=7.0");
map1.put("jurisdictionTaxes", new JurisdictionTax[] { new JurisdictionTax(new TaxRate("bbb")),
new JurisdictionTax(new TaxRate("aaa")) });
map1.put("withholdingJurisdictionTaxes", "[]");
}
static Map<String, Object> map2 = new HashMap<>();
static {
map2.put("taxRate", "rateType=EXCLUSIVE, rate=0.07");
map2.put("taxAmount", "currencyCode=USD, amount=7.0}");
map2.put("jurisdictionTaxes", new JurisdictionTax[] { new JurisdictionTax(new TaxRate("ccc")),
new JurisdictionTax(new TaxRate("aaa")) });
map2.put("withholdingJurisdictionTaxes", "[]");
}
public static void main(String[] args) {
System.out.println("map1 before: " + ((JurisdictionTax[])map1.get("jurisdictionTaxes"))[0]);
System.out.println("map2 before: " + ((JurisdictionTax[])map2.get("jurisdictionTaxes"))[0]);
Arrays.sort((JurisdictionTax[]) map1.get("jurisdictionTaxes"));
Arrays.sort((JurisdictionTax[]) map2.get("jurisdictionTaxes"));
System.out.println("map1 after: " + ((JurisdictionTax[])map1.get("jurisdictionTaxes"))[0]);
System.out.println("map2 after: " + ((JurisdictionTax[])map2.get("jurisdictionTaxes"))[0]);
打印:
map1 before: JurisdictionTax [jurisdictionLevel=null, taxAmount=null, taxRate=TaxRate [rateType=bbb, rate=null]]
map2 before: JurisdictionTax [jurisdictionLevel=null, taxAmount=null, taxRate=TaxRate [rateType=ccc, rate=null]]
map1 after: JurisdictionTax [jurisdictionLevel=null, taxAmount=null, taxRate=TaxRate [rateType=aaa, rate=null]]
map2 after: JurisdictionTax [jurisdictionLevel=null, taxAmount=null, taxRate=TaxRate [rateType=aaa, rate=null]]
带有定义类:
public class JurisdictionTax implements Comparable<JurisdictionTax> {
public JurisdictionTax(TaxRate taxRate) {
super();
this.taxRate = taxRate;
}
public String jurisdictionLevel;
public TaxAmount taxAmount;
public TaxRate taxRate;
public String displayName;
public String jurisdictionName;
public String responsiblePartyReason;
@Override
public int compareTo(JurisdictionTax other) {
// incomplete
return this.taxRate.rateType.compareTo(other.taxRate.rateType);
}
@Override
public String toString() {
return "JurisdictionTax [jurisdictionLevel=" + jurisdictionLevel + ", taxAmount=" + taxAmount + ", taxRate="
+ taxRate + "]";
}
}
等