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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:40:02+00:00 2026-05-27T13:40:02+00:00

I have a variable: ConcurrentHashMap<String, List<AnObjectType>> mapOfList; The List as I understand is not

  • 0

I have a variable:

ConcurrentHashMap<String, List<AnObjectType>> mapOfList;

The List as I understand is not thread safe. But I don’t want to use Synchronized keyword as I actually want concurrent read access and synchronized write to the list and not blocking read and writes.

So I would normally declare Volatile on the variable like so:

volatile List<AnObjectType> varName;

(Although in this case I think this referers to Volatile reference on the list, but what I want is for both the list reference and the content of the list to be volatile.)

But how do I do that within a ConcurrentHashMap construct given that I don’t declare the list as a variable anywhere but within a method?

i.e. List is created within method:

if (!mapOfList.containsKey("ListA")) {
     List<AnObjectType> listA=new ArrayList<AnObjectType>();
     mapOfList.put("ListA", listA);
}

and the list is accessed in another method within the same class:

List<AnObjectType> listA=mapOfList.get("ListA");
if (listA!=null) {
     // Do something concurrent with listA.
}

Sub Question: Would something like this work at all?

ConcurrentHashMap<String,List<AtomicReference<AnObjectType>>>>

Elaboration on the list’s operations:

The list will be accessed via multiple threads reading almost constantly. Write access to the list will be triggered on certain conditions. So what I want is a concurrent access to the List’s content with seldom write operations on the list’s content that should be reflected by all reads after the write operation.

  • 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-27T13:40:02+00:00Added an answer on May 27, 2026 at 1:40 pm

    Use ConcurrentMap#putIfAbsent for creating the List, and access all lists in a lazy getter:

    List<AnObjectType> getList(String key) {
         if (!mapOfList.containsKey(key)) {
         // This list might not end up being the one that gets returned, but
         // that's OK
             mapOfList.putIfAbsent(key, new CopyOnWriteArrayList<AnObjectType>());
         }
         return mapOfList.get(key);
    }
    

    The CopyOnWriteArrayList should give you the synchronized performance you need.

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

Sidebar

Related Questions

If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have variable: String colorName = BLUE; I want to set this color to
I have variable x and I want to see if it contains a string
I have a variable last_login and want to output a message if the last
I have a variable of type Hashmap <String,Integer >. In this, the Integer value
We have variable with text: $text = 'I use something similar to pull the
In a particular situation I need to have variable (character array or std:string) where
I want a two-column div layout, where each one can have variable width e.g.
I have variable length character data and want to store in SQL Server (2005)
May it's very simple question, but I'm stuck here. I have variable val as

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.