我试图MultiKeyMap
在 Java 中更新一组特定键的值,但无法获得如何做到这一点。
假设我想用不同的值(key1,key2,value2)更新(key1,key2,value1)同一组键。
如果我只是执行 put inMultiKeyMap
然后它会创建新行并且不会更新值。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);
}
}
}
替换功能出错
不适用于参数 (String, String, String, List<Double>)
请提供在 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>