I was trying to update the values of set of particular keys in MultiKeyMap
in Java but not able to get how to do that.
Suppose I want to update (key1, key2, value1) same set of key with different value (key1, key2, value2).
If I simply perform put in MultiKeyMap
then it create new row and doesn’t update the value. replace()
is also not working.
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); } } }
Getting error at replace function
not applicable for the arguments (String, String, String, List<Double>)
Please provide solution for updating value in MultiKeyMap.
maven dependency :
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
Advertisement
Answer
You can use put in order to replace (key1,key2,key3) with new value list. Your code run correct with this exemple
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); }