Java Best way to filter a list of objects

I have a list of objects that say Sales. I want only Sales objects whose Product matches those in another list, for example saleProductList.

With the exception of loops, there is a better way to do this.

+3
source share
6 answers

If you already use the Google Guava library, it has the Collections2.filter () method , which will only return elements from which match the given Predicate .

, , , . Java , - . Guava , , , API.

+13

Collections Apache. . , , .

+1

, , . LAZILY , , , .

, filter (...): Iterables.filter(Iterable, Predicate)

+1

.

, . , .

, , , .

0

Google libray:

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

, . ( "" ), Iterable Iterator :

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

( .)

0

Source: https://habr.com/ru/post/1775646/


All Articles