java - Issue with updating multiple values with the same key using HashMap -
i'm having issue 1 key/value pair getting updated properly, other values same key not getting updated. i'm using hashmap consists of list of strings (item nbr) key , values stored list of strings (po nbr), 1 many in regard. below code have implementing hashmap stuck. appreciated.
public void process(list<pobean> exceldata,list<string> errors) throws customexception { map<string,arraylist<string>> ponbritemnbrmap = new hashmap<string, arraylist<string>>(); arraylist<string> ponbrlist = new arraylist<string>(); int recordcount = exceldata.size(); for(pobean bean : exceldata) { if(rowvalidator.validate(bean, errors)) { if (!newerrorinrow(errors)) { ponbrlist.add(bean.getpoid()); ponbritemnbrmap.put(bean.getitemnbr(),bean.getpoid()); } } } list<dutyfees> dutyfeestempdao = myservice.getassocidbypoanditemnbr(new arraylist<string>(ponbritemnbrmap.keyset()),new arraylist<string>(ponbritemnbrmap.values())); for(dutyfees duty : dutyfeestempdao) { double dutyfees = new double(0); if(ponbritemnbrmap.containskey(duty.getitemnbr()) && ponbritemnbrmap.containsvalue(duty.getpoid())) { for(pobean dutybean : exceldata) { if(duty.getpoid().equals(dutybean.getpoid()) && duty.getitemnbr().equals(dutybean.getitemnbr())) { dutyfees = new double(dutybean.getdutyfees()); break; } } boolean result = myservice.updatedutyfeesbyassocid(duty.getassocid(), dutyfees); } } }
you want 1 many code has 1 one
hashmap<string,string> ponbritemnbrmap = new hashmap<string,string>();
one string key 1 string value
the part in italics in contrast part in bold question.
i'm using hashmap consists of list of strings (item nbr) key , values stored list of strings (po nbr), so 1 many in regard.
italics means many many. bold means 1 many.
anyhow, let me give both implementation play with.
public class test { public static void main(string[] args) { system.out.println("= 1 many ="); map<string, list<string>> stringlistmap = new hashmap<>(); string key = "list of 4 strings"; list<string> valueslist = arrays.aslist("one","two","three","four"); stringlistmap.put(key,valueslist); stringlistmap.foreach((strings, strings2) -> system.out.println(strings.tostring() + strings2.tostring())); system.out.println("= many many ="); map<list<string>, list<string>> listlistmap = new hashmap<>(); list<string> keys = arrays.aslist("1","2","3","4"); list<string> values = arrays.aslist("one","two","three","four"); listlistmap.put(keys,values); listlistmap.foreach((strings, strings2) -> system.out.println(strings.tostring() + strings2.tostring())); } }
edit response comment. not sure mean in comment, see couple of problems. first when putting in map putting seems item number , po id, both seem of type single strings , not arraylist. not sure how able compile that. unless getpoid getpoids , returns list. talking line
ponbritemnbrmap.put(bean.getitemnbr(),bean.getpoid());
now regarding
new arraylist<string>(ponbritemnbrmap.values())
you can not initialize array list that.
i don't know signature of getassocidbypoanditemnbr function can't suggest answer. however, if want send map values send directly why put in constructor argument new arraylist<>()?
Comments
Post a Comment