Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • The allocation needed to box the strings to tuple is something I am very keen to avoid. Commented Nov 4, 2011 at 7:51
  • 1
    That would have been an important information to begin with! But using two stacked hash maps will use way more memory. If you are interested in the lowest possible memory consumption try to use a pair of arrays, or a pair of ArrayList<String>. And use the invariant that corresponding strings should be at the same index. Nevertheless, I'd use the original Set interface. Commented Nov 4, 2011 at 7:54
  • Sorry, I did not explain it well. I am ok with some extra memory overhead in storage (entry objects, bucket chains etc, for example); but when contains is called, I do not want to allocate a Tuple, so that it can be searched for in the set. Commented Nov 4, 2011 at 8:09
  • So, you are more concerned about the costs of allocating and or freeing the tuple object? If so, keep in mind that in a generational/copying garbage collection setting, like in a decent JVM, allocation is pretty much for free, and deallocation becomes cheaper if you free an object earlier. A very short lived object is deallocated basically for free. Keeping an object live costs in garbage collection, not freeing it. Commented Nov 4, 2011 at 8:47
  • I agree with most of what you said about allocation; but cheap is not the same as free :) Let me try to illustrate. The code I have to write is a small component that resides alongside another bigger application. My contains calls are done in a background thread, but at a high rate. If I allocate a small object for each such call, although a single allocation is cheap, it can all soon add up to cause adverse effects. Commented Nov 4, 2011 at 15:47