gpt4 book ai didi

java - 基于闭包比较两个无序列表的对象

转载 作者:行者123 更新时间:2023-12-01 12:19:12 26 4
gpt4 key购买 nike

我有两个无序列表。我想要做的是根据我可以通过闭包/指针函数指定的一些条件来比较这两个列表。

我遇到的问题是这些列表中的对象结构可能不同,我想在某些情况下比较某些属性,但在其他情况下比较其他属性。

例如

Class sampleObj{
String attribute1;
List attribute2;
String attribute3;
}

List A
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"]

在这种情况下,列表 A 中的对象 1 等于列表 B 中的对象 3(对象所需的属性都相等),列表 A 中的对象 2 等于列表 B 中的对象 2(属性 3 的值为属性 1) 的子字符串。

因此,我的条件可能基于属性 1 和属性 2 或属性 3 的叉积。意思是,如果 ListA 中的对象 1 和 ListB 中的对象 3 的属性 1 和属性 2 相等,我们可以说这两个对象相等,否则,如果属性 3 与属性 1 的某些条件匹配,那么我们可以说这些对象相等,意味着对象 2 相等listB 可以等于列表 A 中的 object2(在本例中条件是子字符串检查)

一般来说,我正在尝试编写这个库方法,该方法需要两个列表和一个闭包,然后根据传递的闭包让我知道列表 A 中的某些对象是否与列表 B 匹配,反之亦然。

如果这里需要任何问题/澄清或/以及您是否可以引导我走向正确的方向,请告诉我。

最佳答案

如果您想知道的是根据任意标准两个列表之间是否存在匹配,那么您需要的只是以下内容:

def hasMatches(List a, List b, Closure<Boolean> matchCriteria) {
a && b && a.any{ aa -> b.any { bb -> matchCriteria(aa, bb) || matchCriteria(bb, aa) } }
}

那么以下断言都返回 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"]] as SampleObj,
[name: "ObjA2", attribute1: "optionalArg test 2", attribute3: "optionalArg"] as SampleObj
]

List<SampleObj> listB = [
[name: "ObjB1", attribute3: "test4", attribute2: [1,2,3]] as SampleObj,
[name: "ObjB2", attribute3: "optionalArg"] as SampleObj,
[name: "ObjB3", attribute1: "test", attribute2: ["a","b","c"]] as SampleObj
]

// there exists at least one object in list A whose attribute 1 value matches that of at least one object in list B (or vice versa)
assert hasMatches(listA, listB) { SampleObj aa, SampleObj bb -> aa?.attribute1 == bb?.attribute1 }

// there exists at least one object in list B whose attribute 3 value is a substring of the attribute 1 value of at least one object in list A (or vice versa)
assert hasMatches(listA, listB) { SampleObj aa, SampleObj bb -> bb?.attribute3 && aa?.attribute1?.contains(bb.attribute3) }

// there does not exist any object in list A whose attribute 1 value is contained in the attribute 2 list of any object in list B (or vice versa)
assert !hasMatches(listA, listB) { SampleObj aa, SampleObj bb -> aa?.attribute1 && bb?.attribute2?.contains(aa.attribute1) }

另一方面,如果您确实想查看匹配对,那么您需要更广泛的内容:

def findEquivalentPairs(List a, List b, Closure<Boolean> matchCriteria) {
a && 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
}
: []
}

然后,使用相同的三个标准闭包打印结果...

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) }

...产生以下结果:

[[ObjA1, ObjB3], [ObjB3, ObjA1]]
[[ObjA2, ObjB2]]
[]

关于java - 基于闭包比较两个无序列表的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26811861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com