Google馆藏(番石榴库):ImmutableSet/List/Map和Filtering [英] Google Collections (Guava Libraries): ImmutableSet/List/Map and Filtering

查看:118
本文介绍了Google馆藏(番石榴库):ImmutableSet/List/Map和Filtering的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assume that you want to build a copy of an ImmutableSet/List/Map object but filter out some of the original entries. One way to implement that is the following:

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

其中,myObject.EQUALSIterables.filter()操作的谓词.我认为这是一个非常优雅且易于阅读的实现.但是,一个人构建两个列表对象(第一个通过Iterables.filter(...)调用,第二个通过ImmutableList.copyOf(...)调用),效率很低.

where myObject.EQUALS is a predicate for the Iterables.filter() operation. I think this is a pretty elegant and easy-to-read implementation. However, one builds two list objects (first through the Iterables.filter(...) call, second through ImmutableList.copyOf(...)) which is very inefficient.

有人知道一种更有效的方法吗?

Does anybody know of a more efficient way to do it?

我猜最好的办法是将过滤谓词添加到ImmutableSet/List/Map生成器,以便对象只能构造一次.但不幸的是,没有这样的参数.

I guess the best thing would be to add filter predicates to the ImmutableSet/List/Map builders so that the object must be constructed only once. But unfortunately there is no such parameter.

推荐答案

Iterables.filter()的结果只是对myObject中数据的查看:新列表仅由ImmutableList.copyOf()使用提供的过滤迭代器构建由Iterable

The result of Iterables.filter() is just a view over the data in myObject: the new list is only built by ImmutableList.copyOf() using the filtering iterator provided by the Iterable

这篇关于Google馆藏(番石榴库):ImmutableSet/List/Map和Filtering的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆