Google Collections中的延迟不可修改的清单 [英] Lazy non-modifiable list in Google Collections

查看:135
本文介绍了Google Collections中的延迟不可修改的清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个像样的实现一个通用的惰性不可修改的列表实现,以包装我的搜索结果条目。任务的不可修改部分很容易,因为它可以通过 Collections.unmodifiableList()实现,所以我只需要整理懒惰的部分。

I was looking for a decent implementation of a generic lazy non-modifiable list implementation to wrap my search result entries. The unmodifiable part of the task is easy as it can be achieved by Collections.unmodifiableList() so I only need to sort out the the lazy part.

令人惊讶的是, google集合有什么要提供;而来自Apache Commons Collections的LazyList < a>不支持泛型。

Surprisingly, google-collections doesn't have anything to offer; while LazyList from Apache Commons Collections does not support generics.

我发现尝试在google-collections之上构建一个东西,但它似乎是不完整的(例如不支持 size )),过期的(不使用1.0 final编译),并且需要一些外部类,但可以作为一个良好的起点来构建自己的类。

I have found an attempt to build something on top of google-collections but it seems to be incomplete (e.g. does not support size()), outdated (does not compile with 1.0 final) and requiring some external classes, but could be used as a good starting point to build my own class.

有人知道任何好的LazyList实现吗?如果没有,你认为哪个选项更好:

Is anybody aware of any good implementation of a LazyList? If not, which option do you think is better:


  • 编写我自己的实现,基于google集合的ForwardingList,

  • 在Commons Collections LazyList中编写自己的包装器(包装器只会添加泛型,所以我不必在所有地方,只在包装器本身);

  • 只需在顶部写入一些东西;

  • write my own implementation, based on google-collections ForwardingList, similar to what Peter Maas did;
  • write my own wrapper around Commons Collections LazyList (the wrapper would only add generics so I don't have to cast everywhere but only in the wrapper itself);
  • just write something on top of java.util.AbstractList;

欢迎任何其他建议。

编辑:解释为什么我需要一个惰性列表。

explanation why I need a lazy list.

我有一个Lucene搜索结果(TopDocs),这基本上是一堆指针Lucene文档。我的搜索结果类将接受这些指针作为输入,并返回由提取和其他处理的Lucene文档组成的对象列表。通过将所有东西包装到一个惰性列表中,我想确保我不必在不必要的时候进行昂贵的处理。

I have got a Lucene search result (TopDocs) which is basically a bunch of pointers to Lucene documents. My search result class would take these pointers as an input and return a list of objects which are made of extracted and otherwise processed Lucene documents. By wrapping everything into a lazy list I want to ensure I am not doing expensive processing when unnecessary.

推荐答案

以不同的方式。而不是经历懒惰和不可修改,我只是实现 java.lang.Iterable< T> 。实施在 remove()上引发 UnsupportedOperationException

I have actually solved this in a different way. Instead of going through lazy and unmodifiable, I simply implemented java.lang.Iterable<T>. The implementation throws UnsupportedOperationException on remove().

我不得不稍微修改一些其他代码部分,放弃一些东西,但我相信这是最好的选择。 Iterable 允许它放在foreach循环。

I had to slightly modify some other code parts, give up something, but I believe this was the best choice. Iterable allows it to be put on foreach loop.

对不起,如果这不是一个可行的选择对于某人在类似的情况,非常感谢的想法。

Sorry to disappoint if this won't be a viable choice for somebody in a similar situation and thanks very much for the ideas.

这篇关于Google Collections中的延迟不可修改的清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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