简体   繁体   English

Java 中是否有不可变的单链表实现?

[英]Is there an immutable singly linked list implementation in Java?

Coming from functional background, I am looking for equivalent of immutable singly linked list in Java.来自函数式背景,我正在寻找 Java 中不可变单链表的等价物。

Immutable singly linked lists give me freedom to define many lists with common tail.不可变的单链表让我可以自由地定义许多具有共同尾部的列表。 For example, if I have list = [1,2,3] and then I am creating two new lists:例如,如果我有list = [1,2,3]然后我创建了两个新列表:

first = [10 | list]
second = [15 | list]

I am not copying the list.我不是在复制列表。 Internally it looks more like this:在内部它看起来更像这样:

first -> 10 -> 1 -> 2 -> 3 -> null
second -> 15  /|\

I looked at Guava Lists, but I couldn't find information on implementation details.我查看了番石榴列表,但找不到有关实现细节的信息。 As far as I understand, it is a doubly linked list, so efficient prepend operation is impossible (please correct me, if I am wrong about that).据我了解,它是一个双向链表,因此不可能进行有效的前置操作(如果我错了,请纠正我)。

Did you try Functional Java ? 您尝试过Functional Java吗? Also there's similar question , you could use that algorithm and make singly list from double. 也有类似的问题 ,您可以使用该算法并从double中单独列出。

试试Vavr,它是开源的,所以你可以看到内部实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM