Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 136287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:55:28+00:00 2026-05-11T06:55:28+00:00

So I need a 2-dimensional ConcurrentHashMap . It has to be as blazing fast

  • 0

So I need a 2-dimensional ConcurrentHashMap.

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.

Both the ‘x’ and ‘y’ indices are integers with a known range (0 through 40,000,000).

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:

ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ValueObj>> foo;

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 equals() and hashcode()? and will I wind up allocating too many new IntPairs? Could I keep a set of IntPairs 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?


Update:

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.

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 new IntPair(x,y) each time I look up a key. Yes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:55:29+00:00Added an answer on May 11, 2026 at 6:55 am

    It depends on how sparse your (x,y) points are, in the 40,000,000 x 40,000,000 matrix. My guess is that the matrix is going to be quite sparse anyway, so creating a lot of ConcurrentHashMaps is going to be expensive.

    Your (immutable) IntPair suggestion seems more attractive in comparison. As you’ve suggested, you can even cache some of these pairs to improve performance (see Integer.valueOf(int) to see how this can be implemented using a static nested class and a static factory method). Since the hashcode will always be required, you can pre-compute it in the constructor and save it as a final field. To compute equals, you could use the identity equality for objects in the cache, otherwise you’ll need to compare x and y individually.

    EDIT: Here’s the source code (OpenJDK) for Integer.valueOf(int).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function that has a parameter which is a multi-dimensional
I need to read a bi-dimensional array and convert the values into an HTML
I need to convert latitude and longitude values to a point in the 3-dimensional
I need to loop through a set of 2-dimensional array of hidden input fields
I have a two dimensional ArrayList that I need to filter the duplicates out
I have a task where I need to translate a DataTable to a two-dimensional
I have the following three arrays and need to create a new two-dimensional array
I need to store multi-dimensional data consisting of numbers in a manner thats easy
I need to sort a 2 dimensional array of doubles on multiple columns using
I need a dynamically allocated bi-dimensional array of objects. I declared a static pointer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.