Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

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).

like image 933
tkowal Avatar asked Nov 18 '25 16:11

tkowal


1 Answers

Did you try Functional Java? Also there's similar question, you could use that algorithm and make singly list from double.

like image 86
Timofey Avatar answered Nov 21 '25 06:11

Timofey