Ability to adapt Java collections

Are there Java libraries for maps and collections that change their capacity-based presentation strategy? I have an application where we have a lot of cards and sets, but most of the time they are small, usually 6 items or less.

So we were able to extract some good memory improvements by writing some specialized maps and sets that just use arrays for small sizes and then default Java Sets and Maps for more capacity.

However, rolling out our own specialized versions of the kit and maps seems silly when there is already something off the shelf. I've looked at guava and Apache collections and they don't seem to offer anything like this. It appears to be more memory efficient than JDK collections in general, but it is unclear if this will try to minimize memory usage.


source to share

2 answers

You can take a look at Clojure persistent data structures. While the "permanent" part may be overkill for you, it does exactly what you are looking for and still very quickly. There is PersistentArrayMap

one that is promoted until PersistentHashMap

after the collection exceeds 16.



I don't know of any such library.

The problem is that views that use the least amount of memory tend to:

  • be incompatible with the Java Collections APIs, making it difficult to integrate and
  • break the boundaries of abstraction; for example adding link fields to element types.

This makes it difficult to build a general purpose library along these lines. Then we add the problem that a view that adapts to minimize the use of heap space as the collection grows and shrinks will inevitably create a lot more garbage ... and this will have CPU performance implications.

Your approach is interesting, although it doesn't give you anywhere like minimal memory usage. I assume your classes are efficiently wrapping for standard implementation classes as collections get large. If this works for you, I suggest you stick with it.



All Articles