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

huangapple go评论62阅读模式
英文:

How to sort List<Object> in a Map<String, Object> in Java?

问题

I have two maps, Map<String, Object> which looks like:

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=[]
}

I am comparing these two maps using:

Map<String, Object> flatMap1 = FlatMapUtil.flatten(map1);
Map<String, Object> flatMap2 = FlatMapUtil.flatten(map2);
MapDifference<String, Object> difference = Maps.difference(flatMap1, flatMap2);

However, if you look at the second key value in the map, it is a list of objects. The order matters while calculating the difference. So, I would need to sort that particular list before finding the Map.difference.

Can you help me understand how I can sort this List<Object>?

I tried the following:

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());
    }
}

(Note: I've translated the code and provided the relevant portions as requested.)

英文:

I have two maps, Map&lt;String,Object&gt; which looks like:

Map&lt;String, Object&gt; 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&lt;String, Object&gt; 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=[]
}

I am comparing these two maps using

  Map&lt;String, Object&gt; flatMap1 = FlatMapUtil.flatten(map1);
Map&lt;String, Object&gt; flatMap2 = FlatMapUtil.flatten(map2);
MapDifference&lt;String, Object&gt; difference = Maps.difference(flatMap1, flatMap2);

However if you look at the second key value in the map, it is a list of objects. The order matters while calculating the diff. So I would need to sort that particular list before finding the Map.difference.

Can you help me understand how I can sort this List<Object>?

I tried the following:

1. Collections.sort(Arrays.asList(leftMap.get(&quot;jurisdictionTaxes&quot;)));
Error: Required type: List &lt;T&gt;
Provided: List &lt;Object&gt;
2. Arrays.asList(leftMap.get(&quot;jurisdictionTaxes&quot;)).sort((s1, s2) -&gt; s1.get(&quot;jurisdictionLevel&quot;) - s2.get(&quot;jurisdictionLevel&quot;));
Error: Cannot resolve method &#39;get&#39; in &#39;Object&#39;
 public static &lt;K, V&gt; MapDifference&lt;K, V&gt; difference(Map&lt;? extends K, ? extends V&gt; left, Map&lt;? extends K, ? extends V&gt; right) {
if (left instanceof SortedMap) {
SortedMap&lt;K, ? extends V&gt; sortedLeft = (SortedMap)left;
return difference(sortedLeft, right);
} else {
return difference(left, right, Equivalence.equals());
}
}

答案1

得分: 1

I think that this is the key:

Error: Required type: List <T>

The sort needs a comparable class to sort with. Try this to sort the jurisdictionTaxes:

Arrays.sort((JurisdictionTax[]) map1.get(&quot;jurisdictionTaxes&quot;));

Eg:

static Map&lt;String, Object&gt; map1 = new HashMap&lt;&gt;();

static {
    map1.put(&quot;taxRate&quot;, &quot;rateType=EXCLUSIVE, rate=0.07&quot;);
    map1 put(&quot;taxAmount&quot;, &quot;currencyCode=USD, amount=7.0&quot;);
    map1.put(&quot;jurisdictionTaxes&quot;, new JurisdictionTax[] { new JurisdictionTax(new TaxRate(&quot;bbb&quot;)),
            new JurisdictionTax(new TaxRate(&quot;aaa&quot;)) });
    map1.put(&quot;withholdingJurisdictionTaxes&quot;, &quot;[]&quot;);
}

static Map&lt;String, Object&gt; map2 = new HashMap&lt;&gt;();

static {
    map2.put(&quot;taxRate&quot;, &quot;rateType=EXCLUSIVE, rate=0.07&quot;);
    map2.put(&quot;taxAmount&quot;, &quot;currencyCode=USD, amount=7.0}&quot;);
    map2.put(&quot;jurisdictionTaxes&quot;, new JurisdictionTax[] { new JurisdictionTax(new TaxRate(&quot;ccc&quot;)),
            new JurisdictionTax(new TaxRate(&quot;aaa&quot;)) });
    map2.put(&quot;withholdingJurisdictionTaxes&quot;, &quot;[]&quot;);
}

public static void main(String[] args) {
    System.out.println(&quot;map1 before: &quot; + ((JurisdictionTax[])map1.get(&quot;jurisdictionTaxes&quot;))[0]);
    System.out.println(&quot;map2 before: &quot; + ((JurisdictionTax[])map2.get(&quot;jurisdictionTaxes&quot;))[0]);

    Arrays.sort((JurisdictionTax[]) map1.get(&quot;jurisdictionTaxes&quot;));
    Arrays.sort((JurisdictionTax[]) map2.get(&quot;jurisdictionTaxes&quot;));

    System.out.println(&quot;map1 after: &quot; + ((JurisdictionTax[])map1.get(&quot;jurisdictionTaxes&quot;))[0]);
    System.println(&quot;map2 after: &quot; + ((JurisdictionTax[])map2.get(&quot;jurisdictionTaxes&quot;))[0]);
}

prints:

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]]

With defined classes:

public class JurisdictionTax implements Comparable&lt;JurisdictionTax&gt; {
    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 &quot;JurisdictionTax [jurisdictionLevel=&quot; + jurisdictionLevel + &quot;, taxAmount=&quot; + taxAmount + &quot;, taxRate=&quot;
                + taxRate + &quot;]&quot;;
    }
}

etc.

英文:

I think that this is the key:

Error: Required type: List &lt;T&gt;

The sort needs a comparable class to sort with. Try this to sort the jurisdictionTaxes:

	Arrays.sort((JurisdictionTax[]) map1.get(&quot;jurisdictionTaxes&quot;));

Eg:

static Map&lt;String, Object&gt; map1 = new HashMap&lt;&gt;();
static {
map1.put(&quot;taxRate&quot;, &quot;rateType=EXCLUSIVE, rate=0.07&quot;);
map1.put(&quot;taxAmount&quot;, &quot;currencyCode=USD, amount=7.0&quot;);
map1.put(&quot;jurisdictionTaxes&quot;, new JurisdictionTax[] { new JurisdictionTax(new TaxRate(&quot;bbb&quot;)),
new JurisdictionTax(new TaxRate(&quot;aaa&quot;)) });
map1.put(&quot;withholdingJurisdictionTaxes&quot;, &quot;[]&quot;);
}
static Map&lt;String, Object&gt; map2 = new HashMap&lt;&gt;();
static {
map2.put(&quot;taxRate&quot;, &quot;rateType=EXCLUSIVE, rate=0.07&quot;);
map2.put(&quot;taxAmount&quot;, &quot;currencyCode=USD, amount=7.0}&quot;);
map2.put(&quot;jurisdictionTaxes&quot;, new JurisdictionTax[] { new JurisdictionTax(new TaxRate(&quot;ccc&quot;)),
new JurisdictionTax(new TaxRate(&quot;aaa&quot;)) });
map2.put(&quot;withholdingJurisdictionTaxes&quot;, &quot;[]&quot;);
}
public static void main(String[] args) {
System.out.println(&quot;map1 before: &quot; + ((JurisdictionTax[])map1.get(&quot;jurisdictionTaxes&quot;))[0]);
System.out.println(&quot;map2 before: &quot; + ((JurisdictionTax[])map2.get(&quot;jurisdictionTaxes&quot;))[0]);
Arrays.sort((JurisdictionTax[]) map1.get(&quot;jurisdictionTaxes&quot;));
Arrays.sort((JurisdictionTax[]) map2.get(&quot;jurisdictionTaxes&quot;));
System.out.println(&quot;map1 after: &quot; + ((JurisdictionTax[])map1.get(&quot;jurisdictionTaxes&quot;))[0]);
System.out.println(&quot;map2 after: &quot; +    ((JurisdictionTax[])map2.get(&quot;jurisdictionTaxes&quot;))[0]);

prints:

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]]

With defined classes:

public class JurisdictionTax implements Comparable&lt;JurisdictionTax&gt; {
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 &quot;JurisdictionTax [jurisdictionLevel=&quot; + jurisdictionLevel + &quot;, taxAmount=&quot; + taxAmount + &quot;, taxRate=&quot;
+ taxRate + &quot;]&quot;;
}
}

etc

huangapple
  • 本文由 发表于 2023年4月4日 10:08:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定