Skip to main content

Trove's Java collections?

1 reply [Last post]
Joined: 2007-11-14

Does anyone have experience using the Trove collections? We are using an TIntHashSet in a performance sensitive part of our code (since it allows us to avoid boxing the integer key).

I'm considering switching some of our code from using the standard Java collections to using Trove. Before I'd check in, it would have to pass our test suite and improve performance, but I'd like to hear experiences before we spend the time to make the switch.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-10-19


We currently use Trove and it works great. Primitive hash sets are particularly much more efficient in terms of memory usage. For example, TIntHashSet require an int[] and a byte[].

On the other hand, if you rely on java.util.HashSet from the Sun JDK, it requires a java.util.HashMap that has an Entry[] where each Entry object has 4 fields (key, value, reference to next entry, cached hash). The value is a reference to the same object, but the key is a reference to an Integer wrapper object.

Because the trove hash sets use open addressing, the default load factor is lower (0.50f) than the one used by java.util.HashMap (0.75f), but even so it should be much more efficient under normal circumstances.

As usual, there's no better test than your own application performance, so I would recommend testing it and making sure that there's a measurable performance improvement. If the difference is small, it may not be worth it because of the incompatible API (although trove has wrappers that solve this, they defeat the purpose somewhat).

Hope it helps,