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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:26:07+00:00 2026-06-12T02:26:07+00:00

I want to have a Map object to contain specific value types. Map<String,Object> foo

  • 0

I want to have a Map object to contain specific value types.

Map<String,Object> foo = new HashMap<String,Object>();
foo.put("1",new Integer(1));
foo.put("2", new String("hello"):
for (Map.Entry<Integer, Integer> entry : foo.entrySet()) {
    if(entry.getValue() instanceof String) {
    //do something
    }
   else if(entry.getValue() instanceof Double) {
       //throw Exception
    }
}

You can see what i am trying to accomplish. I need specific Value types in my Map object so i don’t have to put lots of if/else statements. How do i accomplish this ?

  • 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-12T02:26:08+00:00Added an answer on June 12, 2026 at 2:26 am

    If you don’t like the idea of having multiple maps, you can use the nice OOP feature called overriding.
    see Override Methods for more information.

    I would probably use composition pattern to wrap the value types and then define a interface for the action depending of the value types.

    public abstract class ValueType {
    
        public abstract void valueSpecificAction(); 
    }
    
    public class DoubleValueType extends ValueType {
    
        private Double value;
    
        public DoubleValueType(Double value) {
                this.value = value;
        }
    
        public void valueSpecificAction() {
            //do double specific actions
        }
    }
    
    public class StringValueType extends ValueType {
    
        private String value;
    
    
        public void valueSpecificAction() {
            //do string specific actions
    
    
         }   
    }
    

    You can then add the objects of type ValueType in the maps.

    Map<String,ValueType> foo = new HashMap<String,ValueType>();
    foo.put("key1", new DoubleValueType(42.0));
    

    later you can get the value, which has the type ValueType with the public method valueSpecificAction() which do the stuff depending on the specific type.

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

Sidebar

Related Questions

I have HashMap object contains a key x-y-z with corresponding value test-test1-test2 . Map<String,String>
I have one List which contain one map i want to iterate is List<Map<String,
I have a map that represents a DB object. I want to get 'well
I have a Map, that I want to persist. The domain object is something
I have one very general object that I want to map to a destination
I have an object: map<A*, string> collection; I would like to call the map::find
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
I have Java Map (out of Strings and Ints) objects that I want to
I have a situation where I want to map a pair of objects to
I want to have a map (which is image-mapped) show green in areas where

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.