I have an immutable set (cast as a Set
) that potentially contains many elements. I need a Collection that contains the elements from that set plu
When you want better performance than a full copy, and you have an ordering over elements, you can use an effectively immutable wrapper around a B+ tree to get good incremental set performance.
Adding an item to a B+ tree requires O(log(n)) time and incremental allocation, not O(n) as you get with ImmutableSet.builder().addAll(...).add(...).build()
. This means that building a set from n incremental adds is O(n*log(n)), not O(sqr(n)).
This answer has a pointer to a jdbm library so it might be worth looking at jdbm:jdbm.