Sunday 15 April 2012

java - Comparing two unordered lists of objects based on a closure -



java - Comparing two unordered lists of objects based on a closure -

i have 2 unordered lists. want compare these 2 lists based on conditions can specify through closure/ pointer function.

what having problem construction of objects in these lists may different , want compare attributes in cases other attributes in other case.

e.g.

class sampleobj{ string attribute1; list attribute2; string attribute3; } list obj1-> attribute1 = "test",attribute2 = ["a","b","c"] obj2 -> attribute1 = "optionalarg test 2",attribute3 = "optionalarg" list b obj1 -> attribute3 = "test4", attribute2 = [1,2,3] obj2 -> attribute3 = "optionalarg" obj3 -> attribute1 = "test",attribute2 = ["a","b","c"]

in case object 1 in list equal object 3 in list b (both required attributes of object equal) , object 2 in list equal object 2 in list b (the value of attribute 3 substring of attribute 1).

so, status based on cross product of attribute 1 , attribute 2 or on attribute 3. meaning if attribute1 , attribute2 equal object 1 lista , object 3 listb, can these 2 objects equal, otherwise if attribute 3 matches status attribute 1 can objects equal meaning object 2 listb can equal object2 list (condition beingness substring check in case)

in general trying write library method take 2 lists , closure , based on closure passed lets me know whether objects in list match list b or vice-versa.

let me know if there questions/clarifications needed here or/and if can guide me in right direction.

if want know whether or not matches exist between 2 lists according arbitrary criteria need following:

def hasmatches(list a, list b, closure<boolean> matchcriteria) { && b && a.any{ aa -> b.any { bb -> matchcriteria(aa, bb) || matchcriteria(bb, aa) } } }

then next assertions homecoming true:

class sampleobj{ string attribute1 list attribute2 string attribute3 string name @override string tostring() { name } } list<sampleobj> lista = [ [name: "obja1", attribute1: "test", attribute2: ["a","b","c"]] sampleobj, [name: "obja2", attribute1: "optionalarg test 2", attribute3: "optionalarg"] sampleobj ] list<sampleobj> listb = [ [name: "objb1", attribute3: "test4", attribute2: [1,2,3]] sampleobj, [name: "objb2", attribute3: "optionalarg"] sampleobj, [name: "objb3", attribute1: "test", attribute2: ["a","b","c"]] sampleobj ] // there exists @ to the lowest degree 1 object in list attribute 1 value matches of @ to the lowest degree 1 object in list b (or vice versa) assert hasmatches(lista, listb) { sampleobj aa, sampleobj bb -> aa?.attribute1 == bb?.attribute1 } // there exists @ to the lowest degree 1 object in list b attribute 3 value substring of attribute 1 value of @ to the lowest degree 1 object in list (or vice versa) assert hasmatches(lista, listb) { sampleobj aa, sampleobj bb -> bb?.attribute3 && aa?.attribute1?.contains(bb.attribute3) } // there not exist object in list attribute 1 value contained in attribute 2 list of object in list b (or vice versa) assert !hasmatches(lista, listb) { sampleobj aa, sampleobj bb -> aa?.attribute1 && bb?.attribute2?.contains(aa.attribute1) }

if, on other hand, want see matching pairs, need bit more extensive:

def findequivalentpairs(list a, list b, closure<boolean> matchcriteria) { && b \ ? a.sum { aa -> def ab = b.findresults { bb -> matchcriteria(aa, bb) ? [aa, bb] : null } ?: [] def ba = b.findresults { bb -> matchcriteria(bb, aa) ? [bb, aa] : null } ?: [] ab + ba } : [] }

then, printing out results using same 3 criteria closures...

println findequivalentpairs(lista, listb) { sampleobj aa, sampleobj bb -> aa?.attribute1 == bb?.attribute1 } println findequivalentpairs(lista, listb) { sampleobj aa, sampleobj bb -> bb?.attribute3 && aa?.attribute1?.contains(bb.attribute3) } println findequivalentpairs(lista, listb) { sampleobj aa, sampleobj bb -> aa?.attribute1 && bb?.attribute2?.contains(aa.attribute1) }

... yields following:

[[obja1, objb3], [objb3, obja1]] [[obja2, objb2]] []

java list groovy

No comments:

Post a Comment