如何< Object>在Java中对Map〈String,Object>中的List进行排序?

wko9yo5t  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(212)

我有两个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());
        }
    }
5m1hhzi4

5m1hhzi41#

我认为这是关键:

Error: Required type: List <T>

排序需要一个类似的类来进行排序。尝试这样对管辖区Taxes进行排序:

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 + "]";
}
    

}

等等

相关问题