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

查看:175
本文介绍了检查对象列表是否有与该对象的给定属性匹配的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 
}

它使用 lambdaj < a>。谢谢。

It uses the lambdaj. Thank you.

推荐答案

为了提高简单iterate-and-test-the-property的性能,数据结构作为列表中对象的辅助索引,并将选择谓词转换为针对该索引的查询。

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.

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

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.

这篇关于检查对象列表是否有与该对象的给定属性匹配的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆