0

Is there anything similar in Trove to Java's Collections.singleton, Collections.singletonList, or singletonMap?

I'm working in a application that uses many of the trove collections to reduce memory foot print. However, I have been unable to find any singleton Trove collections. For example, creating a TLongHashSet with 1 element using the default constructor will result in a TLongHashSet with an underlying array of 23 elements. Even when specifying the initial size and load factor as 1 and 1 will still result in an underlying array of 5 elements (Trove's the lowest prime is 5 based on gnu.trove.impl.PrimeFinder). In additional to the underlying array there are additional class variables that could be removed for a singleton implementation.

Jake Walsh
  • 3,669
  • 5
  • 22
  • 28
  • Why do you think you need a singleton collection? – Jim Garrison Aug 07 '14 at 17:43
  • @JimGarrison for the same reasons that the Java singleton collections exist. For my use case, I'm working with methods that return Trove collections. The result may only contain a single element. I would like to use a Trove singleton collection to reduce memory usage. Granted, the memory savings will be minimal. But if there are existing functions/classes that support trove singletons, I might as well use them. – Jake Walsh Aug 07 '14 at 20:36

1 Answers1

2

No, there isn't in the current version of Trove.

You can use Koloboke collections (ex. HFTC) to reduce memory footprint even more, than with Trove, but assign them to variables/fields of parental JCF interface (e. g. java.util.Map), which interoperate well with JCF's Collections.singleton* implementations.

leventov
  • 14,760
  • 11
  • 69
  • 98