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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:11:09+00:00 2026-06-11T05:11:09+00:00

I want a ConcurrentHashMap that have Integer key and Object value ( some value

  • 0

I want a ConcurrentHashMap that have Integer key and Object value ( some value of map are Integer and another is String).
Is this the correct way to init and use my table ?

ConcurrentHashMap table=new ConcurrentHashMap<Integer, Comparable>();
        table.put(new Integer(1), new String("nodata"));
        table.put(new Integer(2), new Integer(23));
  • 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-11T05:11:11+00:00Added an answer on June 11, 2026 at 5:11 am

    A few points

    • You don’t need to autobox int values and wrapping a String literal is pointless.
    • You need to keep the generic type on the left, but using an interface is preferred.
    • You would only use Comparable for comparing objects. However, you can’t compare objects of different types in general so this is more confusing than useful.

    .

    Map<Integer, Object> table = new ConcurrentHashMap<>();
    table.put(1, "nodata");
    table.put(2, 23);
    

    You might want the following if you need the additional methods ConcurrentMap provides.

    ConcurrentMap<Integer, Object> table = new ConcurrentHashMap<>();
    

    This appears to be a case of object denial. Using an Object is likely to be a better choice.

    class MyData {
        private String text;
        private int number;
    
        MyData(String text, int number) {
            this.text = text;
            this.number = number;
        }
    
        public synchronized String getText() {
            return text;
        }
    
        public synchronized void setText(String text) {
            this.text = text;
        }
    
        public synchronized int getNumber() {
            return number;
        }
    
        public synchronized void setNumber(int number) {
            this.number = number;
        }
    }
    
    MyData data = new MyData("nodata", 23);
    

    For a concurrent version of the same.

    class MyData {
        private final AtomicReference<String> text;
        private final AtomicInteger number;
    
        MyData(String text, int number) {
            this.text = new AtomicReference<String>(text);
            this.number = new AtomicInteger(number);
        }
    
        public String getText() {
            return text.get();
        }
    
        public void setText(String text) {
            this.text.set(text);
        }
    
        public int getNumber() {
            return number.get();
        }
    
        public void setNumber(int number) {
            this.number.set(number);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ConcurrentHashMap in the below format: ConcurrentHashMap<String,ConcurrentHashMap<String,ArrayList>> Now in this map,I want
Want to verify that my understanding of how this works. Have a C++ Class
I want to put a value returned from a function into ConcurrentHashmap like that
want to know why String behaves like value type while using ==. String s1
I have an underlying asynchronous out-of-order query/response system that I want to make synchronous.
I am writing a translator, and have quite a few java String literals that
I have these classes that I use to create objects that I want to
Want to just confirm on following. Please verify if this is correct: 1. As
Want to play a file that on path like this /var/spool/sound/dog.wav present on server.
I have an application that creates hundreds of instances of some objects B and

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.