在java中更新multikeymap中特定键的值

hgc7kmma  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(324)

我试图更新中一组特定键的值 MultiKeyMap 在java中,但无法获得如何做到这一点。
假设我想用不同的值(key1,key2,value2)更新(key1,key2,value1)同一组键。
如果我只是简单地执行输入 MultiKeyMap 然后创建新行,不更新值。 replace() 也不起作用。

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.collections4.map.MultiKeyMap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);

        List<Double> list11 = new ArrayList<Double>();
        list11.add((double) 3);
        list11.add((double) 5000);

        List<Double> list22 = new ArrayList<Double>();
        list22.add((double) 7);
        list22.add((double) 2000);

        MultiKeyMap<String, List<Double>> multiKeyMap = new MultiKeyMap<String, List<Double>>();
        multiKeyMap.put("sourcecode11", "basecode11", "productcode11", list11);
        multiKeyMap.put("sourcecode22", "basecode22", "productcode22", list22);

        String source = "sourcecode11";
        String base = "basecode11";
        String product = "productcode11";

        if (multiKeyMap.containsKey(source, base, product)) {
            List<Double> fees = multiKeyMap.get(source, base, product);
            Double count = fees.get(0);
            Double amount = fees.get(1);

            count +=1;
            amount+=500;
            List<Double> list33 = new ArrayList<Double>();
            list33.add(count);
            list33.add(amount);
            multiKeyMap.
            multiKeyMap.replace(source, base, product, list33);
        }

    }

}

在replace函数中获取错误
不适用于参数(string、string、string、list)
请提供更新multikeymap中值的解决方案。
maven依赖项:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
l3zydbqr

l3zydbqr1#

可以使用put将(键1、键2、键3)替换为新的值列表。您的代码在这个示例中运行正确

List<Double> list11 = new ArrayList<>();
    List<Double> list22 = new ArrayList<>();
    list22.add((double) 7);
    list22.add((double) 2000);

    MultiKeyMap<String, List<Double>> multiKeyMap = new MultiKeyMap<>();
    multiKeyMap.put("sourcecode11", "basecode11", "productcode11", list11);
    multiKeyMap.put("sourcecode22", "basecode22", "productcode22", list22);

    String source = "sourcecode22";
    String base = "basecode22";
    String product = "productcode22";

    if (multiKeyMap.containsKey(source, base, product)) {
        List<Double> fees = multiKeyMap.get(source, base, product);
        System.out.println("before :" + multiKeyMap);
        Double count = fees.get(0);
        Double amount = fees.get(1);
        count += 1;
        amount += 500;
        List<Double> list33 = new ArrayList<>();
        list33.add(count);
        list33.add(amount);
        multiKeyMap.put(source, base, product, list33);
        System.out.println("after :" + multiKeyMap);
    }
fdx2calv

fdx2calv2#

这令人困惑。您通过获取“count”和“amount”来处理列表“fees”,将它们递增,然后将递增的值作为两个新值添加到列表中。请告诉我们那里发生了什么事。
同样令人担忧的是,您似乎将列表视为某种数据封装类。

相关问题