开发者

java Best way to Filter list of object [closed]

开发者 https://www.devze.com 2023-01-25 13:44 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 3 years ago.

Improve this question

I have a list of objects say Sales. I want only the Sales obje开发者_开发问答cts whose Product matches the ones in another list, say saleProductList.

Other than looping, is there a better way to do it.


If you're already using Google's Guava library, it has a Collections2.filter() method that will returns only those items from a collection that match a given Predicate.

However, whether this answers your question depends on what your motivation is for avoiding looping. Since Java collections do not have this functionality built-in, the only way to do it is to iterate over all the elements at some level. Guava does this internally, but it is still doing the same loop that you'd do manually, just dressed up in a nicer API.


You can use Collections methods from Apache commons library. However those methods just do the loop for you. You can't really avoid it when trying to do what you need.


There are functional like alternatives which makes your code simple and easier to understand but they internally might have to iterate through the list. But they LAZILY execute the filtering which is good if there are chances that client might not always use this.

Check if the filter(...) method is right for you: Iterables.filter(Iterable, Predicate)


I'd suggest 2 solutions

  1. use predicates from the jakarta collection framework.
    There a may different predicates. you can combine them and create very sophisticated filters.

  2. Read my article: http://java.dzone.com/articles/useful-abuse, search for sub title "Implementation of Filter pattern".

I hope this will help you.


At some level, looping will inevitably be involved.

If both data structures are lists then the cost will be proportional to the PRODUCT of the 2 lists' lengths. That can be very expensive if the lists are large.

To avoid this, one or both of the lists needs to be represented by some data structure that gives faster lookup than a simple list.


Using Google's collections libray:

List result1 = Lists.newArrayList(Collections2.filter(originalList,filterPredicate));
List result2 = Lists.newLinkedList(Collections2.filter(originalList,filterPredicate));

While these do give you a proper List, they incur the storage overhead and iteration time overhead immediately. If you'd rather defer that ("lazy" evaluation), you can use an Iterable or Iterator for serial access:

Iterable result3 = Iterables.filter(originalList,filterPredicate));  // Pangea's solution
Iterator result4 = Iterators.filter(originalList.iterator(),filterPredicate));

(I leave the type parameterization as an exercise to the reader.)

0

精彩评论

暂无评论...
验证码 换一张
取 消