java:写时复制数据结构?
Java 中是否有任何东西可以实现类似以下
interface MSet<T> extends Iterable<T> {
/**
* return a new set which consists of this set plus a new element.
* This set is not changed.
*/
MSet<T> add(T t);
/**
* return a new set which consists of this set minus a designated element.
* This set is not changed.
*/
MSet<T> remove(T t);
}
编辑的内容:我想要类似 CopyOnWriteArraySet
,除了该类是可变的,并且我想要一个不可变的集合,允许创建新集合。这样做的原因是我需要分发对旧集的引用并保持它们不可变。
编辑2:Scala如何实现 scala.collection .immutable.Set?这就是我需要的行为,只是我不想为此而吸收所有的 Scala。
Is there anything in Java that implements something like the following
interface MSet<T> extends Iterable<T> {
/**
* return a new set which consists of this set plus a new element.
* This set is not changed.
*/
MSet<T> add(T t);
/**
* return a new set which consists of this set minus a designated element.
* This set is not changed.
*/
MSet<T> remove(T t);
}
edit: I want something like CopyOnWriteArraySet
, except that class is mutable and I want something that is an immutable set that allows creation of a new set. The reason for this is that I need to hand out references to the old set and leave them immutable.
edit 2: how does Scala implement scala.collection.immutable.Set? This is the kind of behavior I need, just that I don't want to suck in all of Scala just for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Google Collections 库
Immutable*
来处理您的所有内容不可变的集合需求。我想你需要一个轻量级的包装类 - 这可以通过Forwarding*
(也在 GC 中)轻松完成 - 在添加/删除时生成新的不可变(或可变,无论如何)运营。最后,如果您的修改本身不需要产生新的修改,您可以使用 GC 中的静态收集辅助库(Iterables、Lists、Sets 等)中的各种选项来实现这些操作,以获取视图(重新设置) :并集、交集、过滤器)。编辑:但是,谷歌代码目前非常慢 - 可能需要稍等一下才能检查出来。
Use the Google Collections library
Immutable*
for all your immutable collection needs. I suppose you'll need a light-weight wrapped class - which can be done very easily with aForwarding*
(also in GC) - that spawns new immutable (or mutable, whatever) on add/remove operations. Finally, if your modifications don't need to themselves be allowed to spawn new modifications, you can implement those operations using the various options in the static collection helper libraries in GC (Iterables, Lists, Sets, etc) to get views (re sets : union, intersection, filter).edit: however, google-code is veeeeery slow at the moment - might have to wait a bit to check it out.
CopyOnWriteArraySet 类似。但是,复制是内部的,因此它不会返回对新对象的引用。
CopyOnWriteArraySet is similar. However, the copying is internal, so it does not return a reference to a new object.
Java中有写时复制的数据结构,例如 CopyOnWriteArrayList,但是 API 与您建议的 API 略有不同;它没有返回一个新对象,而是具有与其他集合相同的 API,但只是在内部创建数组的单独副本。
没有现成的类型可以提供您想要的 API;然而,实施起来应该相当简单;只需复制集合,对副本执行变异操作,然后返回它。
There are copy-on-write data structures in Java, for example the CopyOnWriteArrayList, however the API is slightly different than the one you have suggested; rather than returning a new object, it has the same API as the other collections, but merely creates a separate copy of the array internally.'
There is no type out of the box with the API that you want; however, it should be fairly trivial to implement; simply duplicate the collection, perform the mutating operation on the duplicate, and return it.