我有一个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.
更多推荐
发布评论