谓词链接问题
下面是对象结构 -
- 类
A
具有类B
的集合, - 类
B
与类C
具有一对一映射code> - 我在
C
中定义了一个谓词P
要求:返回一个 B
列表,其中至少有一个 C 元素
的谓词 P
返回 true
。
我当前的解决方案有一个 for 循环。我想知道是否可以在不使用 for 循环的情况下完成此操作。
Below is the object structure -
- class
A
has a collection of classB
- class
B
has one to one mapping with classC
- I have a predicate
P
defined inC
Requirement : Return a list of B
which have atleast one of the elements of C
's Predicate P
return true
.
My current solution has a for loop. I was wondering if this can be done without using a for loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么谓词的定义位置很重要?您的意思是它是在 C 上定义的吗?
我的答案与 sMoZely (+1) 非常相似,除了我使用 Iterables.filter() 并组成谓词而不是使用 Collections2,但想法是相同的。我也可能在问题中遗漏了一些东西......
Why does it matter where the Predicate is defined? Do you mean that it's define on C?
My answer is very similar to sMoZely (+1) except i'm using Iterables.filter() and composing the Predicates instead of using Collections2 but the idea is the same. I may also be missing something in the question...
如果我理解正确的话……你不能为 B 创建一个谓词来调用 C 的谓词吗?
即在 B 中...
然后在 A 中,您可以对 B 的集合和这个新谓词使用 Collections2.filter 吗?
If I understand this correctly ... can't you just create a Predicate for B that calls the Predicate for C?
i.e. in B ...
And then in A you can just use Collections2.filter over the collection of B and this new predicate?