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

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer As you have an existing object model and you are… May 11, 2026 at 2:59 pm
  • added an answer This page explains it well: Link May 11, 2026 at 2:59 pm
  • added an answer Math.Round(New FileInfo(FileName).Length / 1024,1) May 11, 2026 at 2:59 pm

Related Questions

As per this website, I wish to represent a Maze with a 2 dimensional
I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax
I have a (somewhat) large truth table / state machine that I need to
I recently wrote a small number-crunching program that basically loops over an N-dimensional grid

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.