tkowal
tkowal

Reputation: 9289

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

Upvotes: 3

Views: 3268

Answers (2)

Navneet kumar
Navneet kumar

Reputation: 1964

Try Vavr it's open source so you can see the internal implementation.

Upvotes: 1

Timofey
Timofey

Reputation: 829

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

Upvotes: 3

Related Questions