Note that there are some explanatory texts on larger screens.

plurals
  1. PO2-D (concurrent) HashMap: 2-property key type? hashmap of hashmaps? [update]
    text
    copied!<p>So I need a 2-dimensional <code>ConcurrentHashMap</code>.</p> <p>It has to be as blazing fast as possible, as I'm going to be adding to and updating its values extremely frequently. It's in a multithreaded application, hence the choice to use ConcurrentHashMap instead of just HashMap.</p> <p>Both the "x" and "y" indices are integers with a known range (0 through 40,000,000).</p> <p>What I need to know is: What's the most efficient way to implement this so it'll be as speedy as possible? The most obvious route is to do a literal 2-D hashmap:</p> <p><code>ConcurrentHashMap&lt;Integer, ConcurrentHashMap&lt;Integer, ValueObj&gt;&gt; foo;</code></p> <p>Or I could make a private class "IntPair" with two properties x and y, and use that as a key... though if I do that, what's the most efficient way to do <code>equals()</code> and <code>hashcode()</code>? and will I wind up allocating too many new <code>IntPair</code>s? Could I keep a set of <code>IntPair</code>s for each x/y I've assigned, and then use a purely reflexive equals() such that I'm just checking for the exact same object instance?</p> <hr> <h2>Update:</h2> <p>Now that I've taken a closer look at Integer.valueOf(int), the specific caching model it uses wouldn't make sense here, since I'm dealing with a very sparse matrix with unpredictable entries. I really need to be caching all those IntPairs which are used, not a prespecified subset.</p> <p>Intuitively, it seems to me that looking up an IntPair in a big map to see if I've already created it would, in fact, be more-or-less the same as just looking it up in the big "2-D" ConcurrentHashMap anyway, wouldn't it? So it seems the solution here is really to just use <code>new IntPair(x,y)</code> each time I look up a key. Yes?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload