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; } }
原文地址:https://www.cnblogs.com/wangbin2188/p/10209787.html
时间: 2024-10-06 07:00:56