java - 谷歌 Collection ( Guava 图书馆): ImmutableSet/List/Map and Filtering

标签 java performance immutability guava

假设您要构建一个 ImmutableSet/List/Map 对象的副本,但要过滤掉一些原始条目。一种实现方法如下:

ImmutableList.copyOf(Iterables.filter(myObject, myObject.EQUALS));

其中 myObject.EQUALSIterables.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/

相关文章:

java - 如何对建议的 Java 语言功能提供反馈?

java - 事件驱动的回合制文本游戏,创建对象

MySQL 状态 "closing tables"占用双倍的处理时间

javascript - TypeScript 中的不可变数据

java - 如何序列化包含不可序列化对象的最终字段

java - 如何在反射中使用递归?

java - 出现错误 : non-static method 'getResources()' cannot be referenced from a static context

MYSQL Performance on subquery count of another table

c# - 在控制台中显示时格式化 IEnumerable<double>

c# - 如何以不可变的方式实现缓存?