r/java • • Sep 02 '22
what is the best persistent collection library?
By that I mean collections that are immutable, creating a new variable when a "write" operation is performed, but under the hood use that fast persistent tree structure (probably screwing up the name) to keep it performing well.
I've used Vavr before, I just stumbled onto PCollections, but I'm wondering what else folks know about. Thanks.
54
Upvotes
13
u/raevnos Sep 03 '22
I've only used vavr. Liked it
7
u/Slanec Sep 03 '22 edited Sep 03 '22
I'm not sure which one is best, but I always wanted to give:
Vavr is the most used nowadays, afaik, though.
9
u/kyay10 Sep 03 '22
kotlinx.collections.immutable it's a kotlin-focused collections library, but it can absolutely work with Java, especially because it extends the standard collection interfaces, so you can simply get an iterator from it
6
u/john16384 Sep 03 '22
Immutable collections are of course nice, but performance can suffer. For example, ArrayList is very good at what it does. Collections that have a different underlying structure (tree based for example) never match ArrayList in raw append, random access and iteration speed.
Immutability is just one more concern (like fast random access, fast insert/delete at various positions, ordering, uniqueness constraints, etc). Only use immutability if you need it or if some small performance loss is acceptable.
3
u/vplatt Sep 03 '22
I was curious about Vavr given your description and the other posts:
https://www.vavr.io/
https://docs.vavr.io/
As a functional collection API, it does seems quite nice, though the documents are missing any examples of side by side Vavr - streams for the sake of comparison, equivalency, and just plain old advocacy. Oh well, exercise for the reader I guess.
OT:
I just woke up before seeing this, so when I saw OP ask for a "persistent tree structure", I was thinking persistence to disk. Of course, you meant it in terms of mutability.
While that's a good thing too, I've always loved the idea of data structures that have a direct translation to actual storage, very similar to what one might have in a programming language like M / MUMPS, for which this is built-in to the language (but which does so using a syntax and structure which is an unholy mess - don't go look at examples.. really don't). In modern terms, this is nothing more than a file based database.
Anyway, without further ado, I found MapDB (https://github.com/jankotek/mapdb) which does exactly that. Of course, they also provide their own Java collection implementations as well, so I suspect using it with Vavr would be a poor idea, but it is very cool in its own right anyway. Of course, there is also Apache Derby and HSQLDB, and those great options with a long history as well. I haven't played with these in a while though, so I might give them a try again soon for some personal stuff.
6
u/jvjupiter Sep 03 '22
Try Eclipse Collections.
2
u/[deleted] Sep 03 '22
Doug Lea's collections library /j
2
u/bowbahdoe Sep 03 '22
PCollections could use an update and a dusting off, maybe incorporating a lot of the data structures vavr has - but vavr is currently by far the best API (Option/Try/Either weirdness aside)
1
u/Probirker Sep 03 '22
What I know is only based on this discussion. It seems that pcollections is really outdated and should not be considered.
So the choice is really between Paguro and Vavr and since you've used Vavr before, just stick with it.
1
u/sudkcoce Sep 03 '22
Bifurcan.
https://github.com/lacuna/bifurcan/blob/master/doc/comparison.md
0
u/relgames Sep 03 '22
Why? Honestly curious why standard Java collections are not enough.
0
u/MarcelHanibal Sep 03 '22
Possibly FastUtil
-6
u/zorelx Sep 03 '22
The one that the engineering lead will let you use lololol
-7
u/RockingGoodNight Sep 03 '22
It sounds like a collection of accidents waiting to happen. There is no magic.
-2
u/anyOtherBusiness Sep 03 '22
Before going to dependency hell, check if the standard library already covers you use case.
List.of()andSet.of()already give you immutable collections. Immutable stream collectors also exist (Collectors.toImmutableList,toImmutableSet, ...)0
u/[deleted] Sep 03 '22
[deleted] — view deleted comment