检查对象列表是否有与该对象的给定属性相匹配的节点(Check whether a List of object has a node which matches a given property of

编程入门 行业动态 更新时间:2024-10-28 17:15:06
检查对象列表是否有与该对象的给定属性相匹配的节点(Check whether a List of object has a node which matches a given property of that object) java

我有一个Leave对象列表,Leave的属性是leaveDate(java.util.Date),leaveTime(int),leaveType(String)。 现在要检查List是否有节点的属性leaveDate与timeStamp匹配,timeStamp是另一个Date对象,我们可以遍历该列表。 有没有其他方法可以做到这一点? 我也有以下条件检查器:

if (Lambda.select(this.fullLeaves, Lambda.having(Lambda.on(Leave.class).getLeaveDate(), Matchers.equalTo(timeStamp))).size() == 0) { //some code }

它使用lambdaj 。 谢谢。

I have a List of Leave object, the properties of Leave are leaveDate (java.util.Date), leaveTime(int), leaveType(String). Now to check whether or not that List has a node whose property leaveDate matches with timeStamp, timeStamp is another Date object, we can Iterate through that list. Is there any other way to do it? I also have the following condition checker :

if (Lambda.select(this.fullLeaves, Lambda.having(Lambda.on(Leave.class).getLeaveDate(), Matchers.equalTo(timeStamp))).size() == 0) { //some code }

It uses the lambdaj. Thank you.

最满意答案

为了改进简单的迭代和测试属性的性能,您必须创建一个数据结构以充当列表中对象的二级索引,并将您的选择谓词转换为针对该索引的查询。

选择谓词的本质将决定哪些索引数据结构是最好的。 如果你只是测试属性的平等,那么一个HashMap就可以。 如果你需要做时间戳比较(之前,之后),那么需要一个TreeMap。

请注意,这里有一个权衡。 二级索引会为您提供更快速的列表搜索,但成本会增加复杂性,而列表添加和删除速度会变慢。 所以,这样的平均列表大小和使用模式将决定一个二级索引是否会提高整体性能。


如果您正在测试的属性是可变的/当对象位于列表中时可能会更改,则每次更改入围对象的属性时都需要更新二级索引。 正确实施会增加额外的成本和复杂性。

To improve on the performance of simple iterate-and-test-the-property, you have to create a data structure to act as a secondary index for the objects in the list, and transform your selection predicate into queries against that index.

The nature of your selection predicates will determine what index data structure(s) are best. If you are going just test for property equality then a HashMap will do. If you need to do timestamp comparisons (before, after) then a TreeMap will be needed.

Note that there is a trade-off here. The secondary index will give you faster list searches, but the cost will be increased complexity, and slower list addition and removal. So, things such average list sizes and usage patterns will determine if a secondary index gives an overall improvement in performance.


If the property you are testing is mutable / might change while the object is in the list, then you would need to update the secondary index each time an enlisted object's property is changed. Implementing this correctly would add significant extra cost and complexity.

更多推荐

本文发布于:2023-07-31 22:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1349025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   节点   与该   相匹配   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!