user373201 user373201 -4 years ago 165
Java Question

java Best way to Filter list of object

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download