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 9303559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:30:47+00:00 2026-06-18T23:30:47+00:00

I posted an answer here where the code demonstrating use of the putIfAbsent method

  • 0

I posted an answer here where the code demonstrating use of the putIfAbsent method of ConcurrentMap read:

ConcurrentMap<String, AtomicLong> map = new ConcurrentHashMap<String, AtomicLong> ();

public long addTo(String key, long value) {
  // The final value it became.
  long result = value;
  // Make a new one to put in the map.
  AtomicLong newValue = new AtomicLong(value);
  // Insert my new one or get me the old one.
  AtomicLong oldValue = map.putIfAbsent(key, newValue);
  // Was it already there? Note the deliberate use of '!='.
  if ( oldValue != newValue ) {
    // Update it.
    result = oldValue.addAndGet(value);
  }
  return result;
}

The main downside of this approach is that you have to create a new object to put into the map whether it will be used or not. This can have significant effect if the object is heavy.

It occurred to me that this would be an opportunity to use Lambdas. I have not downloaded Java 8 n’or will I be able to until it is official (company policy) so I cannot test this but would something like this be valid and effective?

public long addTo(String key, long value) {
  return map.putIfAbsent( key, () -> new AtomicLong(0) ).addAndGet(value);
}

I am hoping to use the lambda to delay the evaluation of the new AtomicLong(0) until it is actually determined that it should be created because it does not exist in the map.

As you can see this is much more succinct and functional.

Essentially I suppose my questions are:

  1. Will this work?
  2. Or have I completely misinterpreted lambdas?
  3. Might something like this work one day?
  • 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-18T23:30:48+00:00Added an answer on June 18, 2026 at 11:30 pm

    UPDATE 2015-08-01

    The computeIfAbsent method as described below has indeed been added to Java SE 8. The semantics appear to be very close to the pre-release version.

    In addition, computeIfAbsent, along with a whole pile of new default methods, has been added to the Map interface. Of course, maps in general can’t support atomic updates, but the new methods add considerable convenience to the API.


    What you’re trying to do is quite reasonable, but unfortunately it doesn’t work with the current version of ConcurrentMap. An enhancement is on the way, however. The new version of the concurrency library includes ConcurrentHashMapV8 which contains a new method computeIfAbsent. This pretty much allows you to do exactly what you’re looking to do. Using this new method, your example could be rewritten as follows:

    public long addTo(String key, long value) {
        return map.computeIfAbsent( key, () -> new AtomicLong(0) ).addAndGet(value);
    }
    

    For further information about the ConcurrentHashMapV8, see Doug Lea’s initial announcement thread on the concurrency-interest mailing list. Several messages down the thread is a followup message that shows an example very similar to what you’re trying to do. (Note however the old lambda syntax. That message was from August 2011 after all.) And here is recent javadoc for ConcurrentHashMapV8.

    This work is intended to be integrated into Java 8, but it hasn’t yet as far as I can see. Also, this is still a work in progress, names and specs may change, etc.

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

Sidebar

Related Questions

I posted a problem and got an answer here . Here is the code
I have tried various link posted on SO,but not got reliable answer. Here is
I recently posted my problem but neither did I get an answer here or
EDIT: I figured out the answer to the original YUI3 question I posted here,
Update : I posted my code solution as an answer down below, which could
An hour ago I posted an answer here which according to me was correct.
I am following answer posted here. OGNL and wildcards working in tiles definitions with
I have taken a manifest from one of the answers posted here in SO
An answer posted for one of my previous questions brings up another question; I
As demonstrated in this answer I recently posted, I seem to be confused about

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.