接受任何类型数据类型的参数

时间:2016-06-23 09:21:42

标签: java

我有这段代码:

public static Map buildMap(Map map){
    Map data = new HashMap();
    Map mapStorage = new HashMap();

    Set<Map.Entry<String, Class<?>>> entryset = map.entrySet();
    for (Map.Entry<String, Class<?>> entry : entryset) {
        String key = entry.getKey();
        Class<?> val = entry.getValue();
        if(key.contains("_")){
            String mapName = key.substring(0, key.indexOf("_"));
            String mapKey = key.substring(key.indexOf("_")+1, key.length());
            Class<?> mapValue = val;
            boolean mapFound = false;
            Set<Map.Entry<String, Map>> entryset1 = mapStorage.entrySet();
            for (Map.Entry<String, Map> entry1 : entryset1) {
                String key1 = entry1.getKey();
                Map val1 = entry1.getValue();
                if(key1.equals(mapName)){
                        val1.put(mapKey, mapValue);
                        mapFound = true;
                }
            }
            if(!mapFound){
                Map m = new HashMap();
                m.put(mapKey, mapValue);
                mapStorage.put(mapName, m);
            }
        }else{
            data.put(key, val);
        }
    }

    Set<Map.Entry<String, Map>> entryset2 = mapStorage.entrySet();
    for (Map.Entry<String, Map> entry2 : entryset2) {
        String key = entry2.getKey();
        Map val = entry2.getValue();
        data.put(key, val);
    }

    return data;
}

演示如何运作:

Map m = new HashMap();
m.put("objid","1234");
m.put("state","CURRENT");
m.put("tdno","789-09483");
m.put("rpu_objid","R3534");
m.put("rpu_state","PENDING");
m.put("realproperty_objid","RP8393");
m.put("realproperty_owner","Charles Lio");
m.put("realproperty_address","USA");

Map data = DataBuilder.buildMap(m);

System.out.println(data);

当您运行上面的代码时,输​​出应返回Map,其值如下所示:

{rpu={objid=R3534, state=PENDING}, realproperty={objid=RP8393, address=USA, owner=Charles Lio}, objid=1234, tdno=789-09483, state=CURRENT}

但不幸的是,它引发了一个错误:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Class

问题:如何更改代码以便接受任何类型的数据类型?

3 个答案:

答案 0 :(得分:2)

只需将Class<?>替换为Object作为下一个:

public static Map buildMap(Map map){
    ...
    Set<Map.Entry<String, Object>> entryset = map.entrySet();
    for (Map.Entry<String, Object> entry : entryset) {
        String key = entry.getKey();
        Object val = entry.getValue();
        if(key.contains("_")){
            String mapName = key.substring(0, key.indexOf("_"));
            String mapKey = key.substring(key.indexOf("_")+1, key.length());
            Object mapValue = val;
            ...

答案 1 :(得分:2)

使用Object而不是class

Set<Map.Entry<String, Class<?>>> entryset = map.entrySet();

Set<Map.Entry<String, Object>> entryset = map.entrySet();

答案 2 :(得分:0)

public static Map buildMap(Map<String, Object> map){
Map<String, Object> data = new HashMap<>();
Map<String, Map<String, Object>> mapStorage = new HashMap<>();

Set<Map.Entry<String, Object>> entryset = map.entrySet();
for (Map.Entry<String, Object> entry : entryset) {
    String key = entry.getKey();
    Object val = entry.getValue();
    if(key.contains("_")){
        String mapName = key.substring(0, key.indexOf("_"));
        String mapKey = key.substring(key.indexOf("_")+1, key.length());
        String mapValue = val;
        boolean mapFound = false;
        Set<Map.Entry<String, Map>> entryset1 = mapStorage.entrySet();
        for (Map.Entry<String, Map> entry1 : entryset1) {
            String key1 = entry1.getKey();
            Map val1 = entry1.getValue();
            if(key1.equals(mapName)){
                    val1.put(mapKey, mapValue);
                    mapFound = true;
            }
        }
        if(!mapFound){
            Map<String, Map<String, Object>> m = new HashMap<>();
            m.put(mapKey, mapValue);
            mapStorage.put(mapName, m);
        }
    }else{
        data.put(key, val);
    }
}

Set<Map.Entry<String, Map>> entryset2 = mapStorage.entrySet();
for (Map.Entry<String, Map> entry2 : entryset2) {
    String key = entry2.getKey();
    Map val = entry2.getValue();
    data.put(key, val);
}

return data;

}