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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:39:21+00:00 2026-06-03T20:39:21+00:00

Java Docs says that, putIfAbsent is equivalent to if (!map.containsKey(key)) return map.put(key, value); else

  • 0

Java Docs says that, putIfAbsent is equivalent to

   if (!map.containsKey(key)) 
      return map.put(key, value);
   else
      return map.get(key);

So if the key exists in the map, it doesn’t update its value. Is this correct?

What if i want to update a keys value based on some criteria? Say expiration time etc.

Would this be a better impl for adding and updating cache?

public void AddToCache(T key, V value)
{
   V local = _cache.putifabsent(key, value);

   if(local.equals(value) && local.IsExpired() == false){
     return;
   }
   // this is for updating the cache with a new value
   _cache.put(key, value);
}
  • 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-03T20:39:23+00:00Added an answer on June 3, 2026 at 8:39 pm

    So it doesnt update a key’s value. is this correct?

    That is correct. It will return the current value that was already in the Map.

    would this be a better impl for adding and updating cache?

    A couple things would make your implementation better.

    1. You shouldn’t use putIfAbsent to test if it exists, you should only use it when you want to ensure if one does not exist then putIfAbsent. Instead you should use map.get to test it’s existence (or map.contains).

        V local = _cache.get(key);
        if (local.equals(value) && !local.IsExpired()) {
            return;
        }
    

    2. Instead of put you will want to replace, this is because a race condition can occur where the if can be evaluated as false by two or more threads in which one of the two (or more) threads will overwrite the other thread’s puts.

    What you can do instead is replace

    When all is said and done it could look like this

    public void AddToCache(T key, V value) {
        for (;;) {
    
            V local = _cache.get(key);
            if(local == null){
                local = _cache.putIfAbsent(key, value);
                if(local == null)
                    return;
            }
            if (local.equals(value) && !local.IsExpired()) {
                return;
            }
    
            if (_cache.replace(key, local, value))
                return;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#99740 example A3.3 says, it says that an object might still not be
Java docs says that a TreeSet keeps its elements ordered internally. Here what does
as someone's who's just starting to get into Android/Java programming, I've read the docs
https://docs.oracle.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis() says: Returns the current time in milliseconds. Note that while the unit of
The docs of Java 7 for FileWriter and FileOutputStream shows that the constructor of
I was reading the JavaDoc for Threadlocal here https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ThreadLocal.html and it says ThreadLocal instances
I am following this tutorial, I'm using netbeans 6.5.1 http://www.netbeans.org/kb/docs/java/gui-db-custom.html When I get to
I am using Bonita Api Java docs(Bonita Api) to get the instanceUUID of process
Java docs says the following about Set interface, can someone please help me understand
I saw this in the java docs: ScheduledAtFixedRate , it says If any execution

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.