Ravi Parekh
Ravi Parekh

Reputation: 5612

convert Enumeration<String> to Enumeration<object>

UPDATE:

private final java.util.Properties tilesPropertyMap = new Properties();   

private class DelegatingServletConfig implements ServletConfig {   

        public String getServletName() {   
            return "TilesConfigurer";   
        }   

    public ServletContext getServletContext() {   
        return servletContext;   
    }   

    public String getInitParameter(String paramName) {   
        return tilesPropertyMap.getProperty(paramName);   
    }

    @Override
    public Enumeration<String> getInitParameterNames() {
       return tilesPropertyMap.keys(); // returns Enumeration<Object>
    }

}   

UPDATE: i am implementing ServletConfig so i have to getInitParameterNames()

how would i convert Enumeration <String> to Enumeration <object>?

Upvotes: 0

Views: 1916

Answers (4)

Ravi Parekh
Ravi Parekh

Reputation: 5612

    @Override
    public Enumeration<String> getInitParameterNames() {
        Enumeration tile = tilesPropertyMap.keys(); // returns Enumeration<Object>
        return tile;            
    }

Upvotes: 0

home
home

Reputation: 12538

My understanding is that you initilized tilesPropertyMap this way (more or less):

tilesPropertyMap = new HashMap<Object, Object>();

The easiest solution would be to properly initialize the HashMap during creation, like this:

tilesPropertyMap = new HashMap<String, Object>();

Now you do not have to cast anything, the method you've shown above would perfectly work. Or did I missunderstand your question?

Upvotes: 1

Bohemian
Bohemian

Reputation: 425198

I assume you mean how do you convert Enumeration<Object> to Enumeration<String>...

You can't.

Instead, make tilesPropertyMap a Map<String, ?> instead of Map<Object, ?>

Upvotes: 0

Markus Lausberg
Markus Lausberg

Reputation: 12257

i would not cast all map keys to String.

if you are really sure, that only strings are inside these keys, please change the map key type from object to string.

new HashMap()<String, Object>;

Casting the hole map keys, could throw a classcastexception and you would have to handle it.

Upvotes: 0

Related Questions