javalistfilterguava

Guava: Why is there no Lists.filter() function?


Is there a reason there's

Lists.transform()

but no

Lists.filter()

?

How do I filter a list correctly? I could use

new ArrayList(Collection2.filter())

of course, but this way it's not guaranteed that my ordering stays the same, if I understand correctly.


Solution

  • It wasn't implemented because it would expose a perilous large number of slow methods, such as #get(index) on the returned List view (inviting performance bugs). And ListIterator would be a pain to implement as well (though I submitted a patch years ago to cover that).

    Since indexed methods can't be efficient in the filtered List view, it's better to just go with a filtered Iterable, which doesn't have them.