1

I would like to store a set of integers efficiently. HashSet in Java is not so efficient in term of memory usage because of the wrapper class. Sorted array of integers would loose the amortized constant time look up. I also need to iterate through each element so BitSet is not great as well.

Benjamin Nguyen
  • 147
  • 1
  • 9
  • 5
    Do you have hard evidence that using a `HashSet` is a problem for your application? – Paul Boddington Nov 06 '15 at 02:50
  • if hashset uses primitive type not the wrapper class, then it is memory efficient but it did not. – Benjamin Nguyen Nov 06 '15 at 02:56
  • I know. From a memory usage perspective, `HashSet` is appalling. Not just because it uses `Integer` rather than `int`, but also because it's backed by a `HashMap`, so every entry has a completely useless value field. There are loads of libraries for primitive collections out there, but unless you are sure that HashSet is not an option, I would stick to the most familiar class. – Paul Boddington Nov 06 '15 at 03:02
  • Right now, a reduced factor of 10 would be savior for me :). Which are those libraries ? You know which one is also clearly more efficient than others ? – Benjamin Nguyen Nov 06 '15 at 03:14
  • 1
    putting the title of this post into google provides a whole bunch of hits with relevant libraries... – jtahlborn Nov 06 '15 at 03:20
  • I did and I got many libraries Trove, SparseArray, Apache Integer Hash Set but confuses me more. If you could point to a post that compares those libraries, that would be super helpful. – Benjamin Nguyen Nov 06 '15 at 03:30
  • @BenjaminNguyen I've never used any of them, I'm afraid. I don't know how reliable this is https://dzone.com/articles/time-memory-tradeoff-example – Paul Boddington Nov 06 '15 at 03:37
  • BitSet is perfectly fine for iterating over integers if the elements are all relatively small and dense; you just use nextSetBit. – Louis Wasserman Nov 06 '15 at 03:40
  • Awesome. This is a really good start for me. – Benjamin Nguyen Nov 06 '15 at 03:44
  • Please read [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. An excessive number of poorly received questions that are off-topic will get you banned from asking questions, and you do not want that do you? –  Mar 03 '16 at 17:55

1 Answers1

0

The widely-used GNU Trove library provides a lot of excellent primitive-type collections

http://trove.starlight-systems.com/

https://bitbucket.org/trove4j/trove

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • do not answer recommendations questions, and especially not with link only answers –  Mar 03 '16 at 17:56