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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:56:14+00:00 2026-05-20T01:56:14+00:00

Using Guava, I would like to have a map that has the following properties:

  • 0

Using Guava, I would like to have a map that has the following properties:

  • A lot of reads, but very few writes.
  • the data doesn’t expire.
  • must be synchronized, so a write is “atomic” and multiple reads don’t interfere with each other.
  • the map should use the MapConstraint API and a few of these MapConstraint are against the content of the map itself (typically if the records or another exists, do not overwrite it: throw an IllegalStateException instead). I see that the MapConstraint interface doesn’t give the Map being constrained.
  • the MapConstraint‘s check must be done inside the synchronization part.

I’ve well thought about using a ReadWriteLock, but I’m wondering if the MapMaker can help me here since I’m not very familiar with that API.

So what are my options?


Edit: My goal is not a simple putIfAbsent: I need to perform several checks against the map before inserting the value, always in the synchronized write.

  • 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-05-20T01:56:14+00:00Added an answer on May 20, 2026 at 1:56 am

    I’m not sure if you can do this easily with the MapConstraint semantics. You could make the MapConstraint aware of the underlying map, by passing it a reference to the map during construction:

    MapConstraints.constrainedMap(map, new MyCustomMapConstraint(map));
    

    But it would be ugly / risky. Somebody could erroneously do:

    MapConstraint constraint = new MyCustomMapConstraint(firstMap);
    Map constrainedMap = MapConstraints.constrainedMap(secondMap, constraint);
    

    Plus, it wouldn’t solve the synchronization problem.


    If I were to do this, I would use the “putIfAbsent” method provided by ConcurrentMap. I would create a ConcurrentMap wrapper using ForwardingConcurrentMap:

    public class ProtectionistMap<K, V> extends ForwardingConcurrentMap<K, V> {
    
        private final ConcurrentMap<K, V> delegate;
    
        public ProtectionistMap(ConcurrentMap<K, V> delegate) {
            this.delegate = checkNotNull(delegate);
        }
    
        @Override
        protected ConcurrentMap<K, V> delegate() {
            return delegate;
        }
    
        @Override
        public V put(K key, V value) {
            V result = putIfAbsent(key, value);
    
            // The second part of the test is necessary when a map may contain null values...
            if (result != null || value == null && containsKey(key)) {
                throw new IllegalArgumentException("Map already had an entry for key " + key);
            }
            return result;
        }
    
        @Override
        public void putAll(Map<? extends K, ? extends V> map) {
            standardPutAll(map);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a computing map (with soft values ) that I am using to
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
Can I easily convert InputStream to BufferedReader using Guava? I'm looking for something like:
I love Google Guava and use it a lot, but there is one method
Using online interfaces to a version control system is a nice way to have
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I'm using Guava-05-snapshot, with Sun's JDK 1.6 The code blows up executing this snippet:
I was using com.google.common.collect.PrimitiveArrays from Google Collections, however I cannot find it in Guava,

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.