假设您要构建一个 ImmutableSet
/List
/Map
对象的副本,但要过滤掉一些原始条目。一种实现方法如下:
ImmutableList.copyOf(Iterables.filter(myObject, myObject.EQUALS));
其中 myObject.EQUALS
是 Iterables.filter()
操作的谓词。我认为这是一个非常优雅且易于阅读的实现。然而,构建两个列表对象(第一个通过 Iterables.filter(...)
调用,第二个通过 ImmutableList.copyOf(...)
)是非常低效的.
有人知道更有效的方法吗?
我想最好的办法是向 ImmutableSet
/List
/Map
构建器添加过滤谓词,以便必须构造对象只有一次。但不幸的是没有这样的参数。
最佳答案
Iterables.filter()
的结果只是 myObject
中数据的 View :新列表仅由 ImmutableList.copyOf() 构建
使用 Iterable
关于java - 谷歌 Collection ( Guava 图书馆): ImmutableSet/List/Map and Filtering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6176918/