- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个无序列表。我想要做的是根据我可以通过闭包/指针函数指定的一些条件来比较这两个列表。
我遇到的问题是这些列表中的对象结构可能不同,我想在某些情况下比较某些属性,但在其他情况下比较其他属性。
例如
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/
以下闭包函数在 javascript 中运行良好。 function generateNextNumber(startNumber) { var current = startNumber;
Swift的闭包(Closures)是一种将功能块和上下文整合并演示在代码中的一种手段。闭包可以捕获并存储其上下文中的变量和常量。与普遍存在于其他语言的匿名函数(如Python的lambda、Java
在本教程中,您将借助示例了解 JavaScript 闭包。 在了解闭包之前,您需要了解两个概念: 嵌套函数 返回函数 JavaScript 嵌套函数 在 JavaScript 中,一个函数也可
在本教程中,您将借助示例了解 JavaScript 闭包。 在了解闭包之前,您需要了解两个概念: 嵌套函数 返回函数 JavaScript 嵌套函数 在 JavaScript 中,一个函数也可
闭包介绍 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。 要理解闭包,首先必须理解Javascript特殊的变量作用域。 1.全局变量和局部变
这个问题已经有答案了: Methods in ES6 objects: using arrow functions (6 个回答) 已关闭 6 年前。 我已经在 stackoverflow 上到处查找
这个问题已经有答案了: How do JavaScript closures work? (86 个回答) 已关闭 9 年前。 我有一个关于 Javascript 闭包的简单问题: 给出了以下函数:
所以我有以下内容: Object a = data.getA(); Object b = data.getB(); Object c = data.getC(); // and so on 这些对象是
现在已经很晚了,我大脑中道格拉斯·克罗克福德居住的部分已经关闭。我尝试了一些方法,但没有达到预期效果。 我有一个 Canvas ,我在其中画了两条线,然后在计时器上淡出它们,但只有循环中的最后一行淡出
因此,我创建了一个变量 car,然后将其分配给一个函数并添加了参数模型、年份。然后在函数内引用参数创建一个对象。 然后创建“闭包”内部函数 yourCar() 并返回其中的外部函数对象“Propert
我正在 Mozilla 开发者网站上阅读关于关闭的解释,并且有点挣扎。请查看 Mozilla 网站上的以下代码。我有点理解它是如何工作的,但我认为我的评论下面的代码也应该工作。为什么一点击18、20就
这个问题在这里已经有了答案: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assig
以下程序返回“本地”,根据我正在阅读的教程,它旨在演示闭包现象` 我不明白的是,为什么最后为了调用parentfunction,将其分配给变量“child”,然后调用“child”。 为什么只写 pa
我读到闭包末尾的()会立即执行。那么,这两者之间有什么区别。我在一些代码中看到了第一个用法。 谢谢。 for (var a=selectsomeobj(),i=0,len=a.length;i
代码如下 var collection = (function (){ var x = 0; return {
我仍然对 JavaScript 中的闭包概念感到困惑。我明白闭包是内部函数在母函数返回后访问在其母函数中创建的变量的能力。但是我仍然很困惑,如果我们可以在函数内部创建一个变量,为什么我们必须创建内部函
我搜索了很多主题并没有找到答案,或者问题太复杂了。所以没关系。这是我的第一个问题。 这是 SQL SELECT parent.*, ( SELECT COUNT(*) FROM
有 JS 高手可以解释为什么会这样吗: $$={} (function(x){ x.newModule = { func: function(){...} };
在此示例中,我尝试按值传递,但传递的是引用。 for (int i = 0; i new PhoneJobTest(i); t.Start(); } 这可以像这样补救: for (int
从 $.each() 中访问 this.rules 变量的最佳方式是什么?任何关于原因/方式的解释也会有帮助! app.Style = function(node) { this.style
我是一名优秀的程序员,十分优秀!