java泛型方法返回泛型结果

 

public class Test {

    static HashMap<String, String> sMap = new HashMap<String, String>();

    public static void main(String[] args) {

        sMap.put("1", "23");
        sMap.put("2", "ad");
        sMap.put("3", "1.0");
        sMap.put("4", "false");
        Integer i = getUserInfoByKey("1", Integer.class, 1);
        String s = getUserInfoByKey("2", String.class, "null");
        Float f = getUserInfoByKey("3", Float.class, -0.1f);
        Boolean b = getUserInfoByKey("4", Boolean.class, true);
        
    }

    protected static <T> T getUserInfoByKey(String key, Class<T> clazz, T deffault) {
        T ret = null;
        String v = sMap.get(key);
        if (deffault instanceof Integer) {
            ret = (clazz.cast(Integer.parseInt(v)));
        } else if (deffault instanceof String) {
            ret = clazz.cast(v);
        } else if (deffault instanceof Double) {
            ret = clazz.cast(Double.parseDouble(v));
        } else if (deffault instanceof Float) {
            ret = clazz.cast(Float.parseFloat(v));
        } else if (deffault instanceof Long) {
            ret = clazz.cast(Long.parseLong(v));
        } else if (deffault instanceof Boolean) {
            ret = clazz.cast(Boolean.parseBoolean(v));
        }
        if (null != ret) {
            return ret;
        }
        return deffault;
    }
}

 

posted @ 2019-01-02 17:33  Mars.wang  阅读(14961)  评论(0编辑  收藏  举报