当前位置: 动力学知识库 > 问答 > 编程问答 >

java - An argument that accepts any kind of Data Type

问题描述:

I have this code:

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;

}

Demo on how it works:

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);

When you run the code above, the output should return a Map whose value is something like this:

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

But unfortunately, it throws an error:

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

QUESTION: How to change the code so that it will accept ANY TYPE OF DATA TYPE?

网友答案:

Simply replace Class<?> with Object as next:

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;
            ...
网友答案:

Use Object instead of class

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

to

Set<Map.Entry<String, Object>> entryset = map.entrySet();
网友答案:
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;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: