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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:05+00:00 2026-06-11T08:50:05+00:00

I need a data structure to store different type of objects.E.g. String , Boolean

  • 0

I need a data structure to store different type of objects.E.g. String, Boolean and other classes.
Is using a Map<String, Object> where using the key you get the according object which assumes that you know how to cast it a good practice?
Is there a better solution?

  • 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-11T08:50:07+00:00Added an answer on June 11, 2026 at 8:50 am

    That’s a perfect use case for a PropretyHolder I wrote a while ago. You can read in length about it on my blog. I developed it with immutability in mind, feel free to adapt it to your needs.

    In general I’d say if you want to profit from type safety in Java you need to know your keys. What I mean by that – it will be hardly possible to develop type safe solution where keys come from external source.


    Here’s a special key that knows type of its value (it’s not complete please download the source for complete version):

    public class PropertyKey<T> {
        private final Class<T> clazz;
        private final String name;
    
        public PropertyKey(Class<T> valueType, String name) {
            this.clazz = valueType;
            this.name = name;
        }
    
        public boolean checkType(Object value) {
            if (null == value) {
                return true;
            }
            return this.clazz.isAssignableFrom(value.getClass());
        }
    
        ... rest of the class
    
    }
    

    Then you develop a data structure that utilizes it:

    public class PropertyHolder {
    
        private final ImmutableMap<PropertyKey<?>, ?> storage;
    
        /**
         * Returns value for the key of the type extending-the-one-declared-in-the {@link PropertyKey}.
         * 
         * @param key {@link PropertyKey} instance.
         * @return Value of the type declared in the key.
         */
        @SuppressWarnings("unchecked")
        public <T extends Serializable> T get(PropertyKey<T> key) {
            return (T) storage.get(key);
        }
    
        /**
         * Adds key/value pair to the state and returns new 
         * {@link PropertyHolder} with this state.
         * 
         * @param key {@link PropertyKey} instance.
         * @param value Value of type specified in {@link PropertyKey}.
         * @return New {@link PropertyHolder} with updated state.
         */
        public <T> PropertyHolder put(PropertyKey<T> key, T value) {
            Preconditions.checkNotNull(key, "PropertyKey cannot be null");
            Preconditions.checkNotNull(value, "Value for key %s is null", 
                    key);
            Preconditions.checkArgument(key.checkType(value), 
                    "Property \"%s\" was given " 
                    + "value of a wrong type \"%s\"", key, value);
            // Creates ImmutableMap.Builder with new key/value pair.
            return new PropertyHolder(filterOutKey(key)
                    .put(key, value).build());
        }
    
        /**
         * Returns {@link Builder} with all the elements from the state except for the given ket.
         * 
         * @param key The key to remove.
         * @return {@link Builder} for further processing.
         */
        private <T> Builder<PropertyKey<? extends Serializable>, Serializable> filterOutKey(PropertyKey<T> key) {
            Builder<PropertyKey<? extends Serializable>, Serializable> builder = ImmutableMap
                    .<PropertyKey<? extends Serializable>, Serializable> builder();
            for (Entry<PropertyKey<? extends Serializable>, Serializable> entry : this.storage.entrySet()) {
                if (!entry.getKey().equals(key)) {
                    builder.put(entry);
                }
            }
            return builder;
        }
    
        ... rest of the class
    
    }
    

    I omit here a lot of unnecessary details please let me know if something is not clear.

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

Sidebar

Related Questions

What will be the best data structure to store different types of objects that
I am beginner in C++, I need to know which data structure to store
I need to write a data structure that can return a different Price for
I need to store a tree data structure in my database, for which I
I need to store/write information on different and multiple data support in my program
I need a data structure that holds unique values (like a set), but also
I need a data structure which can handle the following: date_from (datetime) date_to (datetime)
I need a data structure which will support the following operations in a performant
I need a data structure that acts like a SortedDictionary<int, double> but is sorted
I need to have data structure, each element in which is accessible by pair

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.