Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 856
Fabian Zeindl Avatar asked Dec 10 '11 18:12

Fabian Zeindl


1 Answers

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.

like image 82
Dimitris Andreou Avatar answered Oct 08 '22 05:10

Dimitris Andreou