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

  • Home
  • SEARCH
  • 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 7801397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:56:17+00:00 2026-06-02T00:56:17+00:00

I’d like to use a map that would be equivalent to ConcurrentMap (I want

  • 0

I’d like to use a map that would be equivalent to ConcurrentMap (I want the equivalent of the putIfAbsent method) but that would not force me to create the object beforehand.

For example when I do this:

m.putIfAbsent( key, new CyclingArray() );

I may end up creating a new CyclingArray (whatever that is) object for nothing.

Of course I realize I could lock up the entire map but that would be defeating the whole point of a ConcurrentMap.

Could something like the following work conceptually?

   m.putIfAbsent( key, new Callback<CyclingArray>() {
     @Override
     public CyclingArray provide() {
       return new CyclingArray();  // only called if the key wasn't already present
     }         
   }

Do you know of any library offering a map that:

  1. offer an “interface” like the one offered by ConcurrentMap, including a putIfAbsent method.
  2. does only lock on the segment we’re going to work with (like the ConcurrentHashMap implementation does, for example)
  3. allows to optionally create the value, if and only if the key wasn’t already present and hence dodge useless garbage generation.
  4. does not force me to use first a containsKey and then a putIfAbsent because this, also, somehow defeats the purpose of putIfAbsent.

Note that I’m not asking if the above example can be done with a ConcurrentMap (it cannot AFAIK).

I was thinking about extending ConcurrentHashMap and overloading putIfAbsent with the callback version but sadly ConcurrentHashMap internally uses a final Segment class.

Before re-inventing the wheel I’d like to know if there are any maps out there already offering a similar functionality.

  • 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. Editorial Team
    Editorial Team
    2026-06-02T00:56:19+00:00Added an answer on June 2, 2026 at 12:56 am

    This is a common use case you are looking for, its called memoization. I would look at MapMaker

    You would be able to create a computingMap and put your creating function there:

     ConcurrentMap<Key, CyclingArray> graphs = new MapMaker()
           .concurrencyLevel(32)
           .makeComputingMap(
               new Function<Key, CyclingArray>() {
                    public CyclingArray  apply(Key key) {
                        return new CyclingArray();  // only called if the key wasn't already    
                    }              
               });
    

    Here the Function will only be called if the Key is not present

    And I do know future plans in Java have a computingMap type interface will come with standard Java, unfortunately at this point you will have to delegate to google-collections.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.