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

  • Home
  • SEARCH
  • 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 8832665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:35:09+00:00 2026-06-14T08:35:09+00:00

Problem: I am trying to find the java type of a map’s key and

  • 0

Problem:

I am trying to find the java type of a map’s key and value, but without having to iterate over the map and then using instanceof, and want to know it even if the map is empty.

The context:

CKEditorConfig which can be downloaded from here (http://ckeditor.com/download) is implemented using a map. But you cannot set this map using injection because setter method is not provided, and there’s no constructor provided to set it using constructor injection. However, there are five addConfigValue method (see below), which I can use to add those values.

public void addConfigValue(final String key, final Number value);
public void addConfigValue(final String key, final String value);
public void addConfigValue(final String key, final Boolean value);
public void addConfigValue(final String key, final Map<String, ? extends Object> value);
public void addConfigValue(final String key, final List<? extends Object> value);

I will be using ckeditor in a couple of places in my j2ee/java web app, and so I figured I should create a factory class that returns the settings needed to initialize the editor by some name. But I wanted to externalize these config and set them in the spring context file. so since I cannot create the config object I have to use regular maps, and then build the config object using the map. but I need to validate those maps coming from spring before building the object.

This is my method for building the config object:

    @SuppressWarnings("unchecked")
    public CKEditorConfig buildConfigFromMap(Map<String, Object> configMap) {
        CKEditorConfig config = new CKEditorConfig();
        for (String key : configMap.keySet()) {
            Object val = configMap.get(key);
            if (val instanceof Number)
                config.addConfigValue(key, (Number) val);
            else if (val instanceof String)
                config.addConfigValue(key, (String) val);
            else if (val instanceof Boolean)
                config.addConfigValue(key, (Boolean) val);
            else if (val instanceof List) {
                config.addConfigValue(key, (List<Object>) val);
            } else if (val instanceof Map) {
                // TODO
            }
            throw new IllegalArgumentException("invalid map value types");
        }
            return config;
    }

And as you can see the if the object is a map, I need to be able to validate if its key is a String. I know I can do something like this:

} else if (val instanceof Map) {
    for (Object mapKey : ((Map<Object, Object>) val).keySet()) {
    if (!(mapKey instanceof String))
        throw new IllegalArgumentException("invalid map value types");
    }
config.addConfigValue(key, (Map<String, Object>) val);
}

but the whole exercise makes me think I am not doing the right thing? any suggestions? I am open to using a totally different approach. I appreciate your input.

  • 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-14T08:35:10+00:00Added an answer on June 14, 2026 at 8:35 am

    It’s impossible, Java generics are erased at runtime.
    In fact they only exist in the compiler (and the source code).

    You cannot inspect the actual types of keys and values and deduce the generics used for the Map from that.

    You can only infer generics in source code for Maps under your control.

    It seems to me you want to force Spring to only supply Map<String, ?>.
    Here is a workaround :

    public class StringKeyMapEntry { public String key; public Object value; }
    

    inject List<StringKeyMapEntry> or StringKeyMapEntry[] (not sure if Spring can do arrays) and convert it to Map<String, ?>

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

Sidebar

Related Questions

i'm trying to find a way to match this problem but i'm LOST i
Googled everything, but can't find solution for my problem. When i'm trying to deploy
OK, this is probably very basic, but I can't find the problem. I'm trying
I'm trying to find a simple solution to a specific problem, which is a
I have been trying to find ways to solve the problem. Firebug said syntax
I am trying to find a cross linux distribution solution to the problem of
I've been trying to find a proper solution to my problem for several days
I've got stuck in a problem with gflags when trying to find some memory
My problem is: There is a collection of users. Im trying to find, does
I am trying to solve this SPOJ problem . The question asks to find

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.