本文介绍了番石榴:为什么没有Lists.filter()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个原因是
$ b $ pre $ Lists.transform()

但是没有

$ p $ Lists.filter()



如何正确过滤列表?我可以使用

  new ArrayList(Collection2.filter())

当然,但是这样做并不能保证我的排序保持不变,如果我理解正确的话。

解决方案

它没有实现,因为它会在返回的列表视图(邀请性能错误)上暴露大量的慢方法,例如#get(index)。而ListIterator也会很难实现(尽管我提交了一个

由于索引方法在过滤的List视图中效率不高,最好使用过滤后的Iterable,而不是Iterable有他们。


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.

解决方案

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.

这篇关于番石榴:为什么没有Lists.filter()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:12