java过滤对象列表的最佳方法 [英] java Best way to Filter list of object

查看:392
本文介绍了java过滤对象列表的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象列表说Sales。我只想要Sales对象,其Product与另一个列表中的对象匹配,比如saleProductList。

I have a list of objects say Sales. I want only the Sales objects whose Product matches the ones in another list, say saleProductList.

除了循环之外,还有更好的方法。

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

推荐答案

如果您已经在使用Google的Guava库,那么它有 Collections2.filter()方法仅返回来自a的项目与给定谓词匹配的集合

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.

但是,这是否能解答您的问题取决于您避免循环的动机。由于Java集合没有内置此功能,因此方法是在某个级别迭代所有元素。 Guava在内部执行此操作,但它仍然使用您手动执行的相同循环,只需打扮成更好的API。

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.

这篇关于java过滤对象列表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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