java java过滤对象列表的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4215645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java Best way to Filter list of object
提问by user373201
I have a list of objects say Sales. I want only the Sales objects whose Product matches the ones in another list, say saleProductList.
我有一个对象列表,比如 Sales。我只想要产品与另一个列表中的产品匹配的 Sales 对象,比如 saleProductList。
Other than looping, is there a better way to do it.
除了循环,有没有更好的方法来做到这一点。
回答by Andrzej Doyle
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.
如果您已经在使用 Google 的 Guava 库,它有一个Collections2.filter()方法,该方法将仅返回集合中与给定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 onlyway 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.
但是,这是否能回答您的问题取决于您避免循环的动机。由于 Java 集合没有内置此功能,因此唯一的方法是在某个级别迭代所有元素。Guava 在内部执行此操作,但它仍然执行与您手动执行相同的循环,只是换上了更好的 API。
回答by AlexR
I'd suggest 2 solutions
我建议2个解决方案
use predicates from the jakarta collection framework.
There a may different predicates. you can combine them and create very sophisticated filters.Read my article: http://java.dzone.com/articles/useful-abuse, search for sub title "Implementation of Filter pattern".
使用来自雅加达集合框架的谓词。
可能有不同的谓词。您可以组合它们并创建非常复杂的过滤器。阅读我的文章:http: //java.dzone.com/articles/useful-abuse,搜索副标题“过滤模式的实现”。
I hope this will help you.
我希望这能帮到您。
回答by shoebox639
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.
您可以使用Collections
Apache 公共库中的方法。然而,这些方法只是为你做循环。在尝试做您需要的事情时,您无法真正避免它。
回答by Aravind Yarram
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)
检查 filter(...) 方法是否适合您:Iterables.filter(Iterable, Predicate)
回答by Stephen C
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.
如果两个数据结构都是列表,那么成本将与两个列表长度的 PRODUCT 成正比。如果列表很大,那可能会非常昂贵。
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.
为了避免这种情况,需要用某种数据结构来表示一个或两个列表,这样查找速度比简单列表更快。
回答by Anm
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:
虽然这些确实为您提供了一个合适的 List,但它们会立即产生存储开销和迭代时间开销。如果您宁愿推迟(“懒惰”评估),您可以使用 Iterable 或 Iterator 进行串行访问:
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.)
(我将类型参数化留给读者作为练习。)