1 使用jdk1.8 object转换为其他类型的工具类
替换原来的
String.valueOf(111); //转换为字符串
jdk1.8使用方法
@FunctionalInterface
public static interface Converter<F, T> {
T convert(F from);
}
//转换object为String的方法 public static Converter<Object, String> converterStr = (from) -> { if (from == null) { return ""; } return String.valueOf(from); }; //转换object为int的方法 public static Converter<Object, Integer> converterInt = (from) -> { if (from == null) { return null; } return Integer.valueOf(converterStr.convert(from)); }; public static Converter<String, Integer> converterInt1 = Integer::valueOf;
对应的Test类
public static void main(String[] args) {
Object obj=null;
System.out.println(ConverterConstant.converterStr.convert(obj));
}