开发者

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 wou开发者_开发技巧ld i convert Enumeration <String> to Enumeration <object>?


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?


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.


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, ?>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜