在 java 中使用的好的持久集合框架是什么? [英] what's a good persistent collections framework for use in java?

查看:19
本文介绍了在 java 中使用的好的持久集合框架是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

持久集合是指像 clojure 中的集合.

By persistent collections I mean collections like those in clojure.

例如,我有一个包含元素 (a,b,c) 的列表.对于普通列表,如果我添加 d,我的原始列表将以 (a,b,c,d) 作为其元素.使用持久列表,当我调用 list.add(d) 时,我会返回一个新列表,包含 (a,b,c,d).但是,该实现会尽可能在列表之间共享元素,因此与简单地返回原始列表的副本相比,它的内存效率要高得多.它还具有不可变的优点(如果我持有对原始列表的引用,那么它将始终返回原始的 3 个元素).

For example, I have a list with the elements (a,b,c). With a normal list, if I add d, my original list will have (a,b,c,d) as its elements. With a persistent list, when I call list.add(d), I get back a new list, holding (a,b,c,d). However, the implementation attempts to share elements between the list wherever possible, so it's much more memory efficient than simply returning a copy of the original list. It also has the advantage of being immutable (if I hold a reference to the original list, then it will always return the original 3 elements).

这在其他地方都有更好的解释(例如 http://en.wikipedia.org/wiki/Persistent_data_structure).

This is all explained much better elsewhere (e.g. http://en.wikipedia.org/wiki/Persistent_data_structure).

无论如何,我的问题是...提供此功能以在 Java 中使用的最佳库是什么?我可以以某种方式使用 clojure 集合吗(直接使用 clojure 的除外)?

Anyway, my question is... what's the best library for providing this functionality for use in java? Can I use the clojure collections somehow (other that by directly using clojure)?

推荐答案

直接使用Clojure中的即可.虽然显然您可能不想使用它自己的语言,但您仍然可以直接使用持久集合,因为它们都只是 Java 类.

Just use the ones in Clojure directly. While obviously you might not want to use the language it's self, you can still use the persistent collections directly as they are all just Java classes.

import clojure.lang.PersistentHashMap;
import clojure.lang.IPersistentMap;

IPersistentMap map = PersistentHashMap.create("key1", "value1");

assert map.get("key1").equals("value1");
IPersistentMap map2 = map.assoc("key1", "value1");

assert map2 != map;
assert map2.get("key1").equals("value1");

(免责声明:我实际上还没有编译该代码:)

(disclaimer: I haven't actually compiled that code :)

缺点是集合没有类型化,即它们没有泛型.

the down side is that the collections aren't typed, i.e. there are no generics with them.

这篇关于在 java 中使用的好的持久集合框架是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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