我有两个Map,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
Map<String, Object> flatMap1 = FlatMapUtil.flatten(map1);
Map<String, Object> flatMap2 = FlatMapUtil.flatten(map2);
MapDifference<String, Object> difference = Maps.difference(flatMap1, flatMap2);
然而,如果你看看map中的第二个键值,它是一个对象列表。在计算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());
}
}
1条答案
按热度按时间5m1hhzi41#
我认为这是关键:
排序需要一个类似的类来进行排序。尝试这样对管辖区Taxes进行排序:
例如:
打印:
对于已定义的类:
等等