Check whether a List of object has a node which matches a given property of that object

╄→гoц情女王★ 提交于 2019-12-13 20:15:33

问题


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.


回答1:


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.




回答2:


When searching through a List of objects, you can't do better than iterating over the objects in O(n) time.

There are two other options, but they require that you use another data structure - either instead of the List, or in addition to it:

  1. Use an array of Leave objects, and keep it sorted by the timestamp. This way you can search for the right Leave in O(log(n)) time. You pay for this with higher insertion time - since you must insert the Leave in the right place, and extend the array size as necessary - with a List you can just append a Leave in O(1).

  2. Use a Map, where the key is the timestamp. This way you can find the Leave you're looking for in O(1) time.

Option two is simpler to implement. In any case, the choice depends on how you use the data structure - what's the insert-to-seek ratio? If you do many more inserts, then it may be best to stay with the List. If you do many more seeks, then go for the Map.

It also depends on how large your typical List is. If it's just hundreds of objects, you won't feel much difference either way.

Finally, the best bet is to test the various implementations under your specific usage pattern.



来源:https://stackoverflow.com/questions/5619920/check-whether-a-list-of-object-has-a-node-which-matches-a-given-property-of-that

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!