java - update list according to complex logic -
i'm having following code , need add instance (type object) item object list(done in last if) need find full key match . fromprop , toprop field type object in instance (are keys can username , customer ,number,f1 etc ). code working on first match field (in last if statement), if first match found add data item list want find full key match i.e if have 3 keys when match add 'toentityinstance' item object. how should ?
for (object fromentityinstance : fromentityinstances) { list<object> itemobject = new arraylist<object>(); (string[] prop : deppropmappings) { // properties related keys fromprop = prop[0]; toprop = prop[1]; object fromvalue = getinstancevalue(fromprop, fromentityinstance); (object toentityinstance : toentityinstances) { object tovalue = getinstancevalue(toprop, toentityinstance); if (fromvalue.equals(tovalue)) { itemobject.add(toentityinstance); } } }
for example entities can fromentityinstance
f1=mark f2=abc f3=test1 f4=test1 f5=test1
toentityinstance
f1=mark f2=abc f3=test1 f4=test1 f5=test1 f1=mark f2=abc f3=test1 f4=test1 f5=test1 f1=jhon f2=yyyy f3=test1 f4=test1 f5=test1
and in deppropmappings have 2 instances keys
first instance
fromprop = f1; toprop = f1;
second instance
fromprop = f2; toprop = f2;
so need add 2 first enities (where f1=mark & f2=abc ) toentityinstance itemobject . code work if found match between f1 ignore f2...
i think count num of key @ begining , if num of key are indentica l update list not sure how that
your loop nesting has wrong order. current code add toentityinstance
list twice if both deppropmappings
match. try this:
for (object fromentityinstance : fromentityinstances) { list<object> itemobject = new arraylist<object>(); (object toentityinstance : toentityinstances) { boolean matches = true; (string[] prop : deppropmappings) { // properties related keys fromprop = prop[0]; toprop = prop[1]; object fromvalue = getinstancevalue(fromprop, fromentityinstance); object tovalue = getinstancevalue(toprop, toentityinstance); // note: make sure fromvalue cannot null. if cannot // guaranteed, add check here. if (!fromvalue.equals(tovalue)) { // if value mismatches, can stop checking remaining // properties. matches = false; break; } } if (matches) { // properties match itemobject.add(toentityinstance); } } }
Comments
Post a Comment