一、object转longer
在一些业务场景下,可能需要将object类型转换为longer类型。object类型是所有数据类型的基类,也就是说它可以表示任何数据类型。longer类型是long的包装类,其可以表示比long类型更大的数值范围。下面来看一下如何将object转换为longer。
public static Long objectToLonger(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof Long) {
return (Long) obj;
}
if (obj instanceof Integer) {
return ((Integer) obj).longValue();
}
if (obj instanceof String) {
try {
return Long.valueOf((String) obj);
} catch (NumberFormatException e) {
return null;
}
}
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to Long");
}
上述代码实现了将object类型转换为longer类型的方法,包括了对于null、Long、Integer、String等类型的处理。其中,如果object为null,返回null;如果object是Long或Integer类型,则直接将其转为Long类型返回;如果object是String类型,可以通过Long.valueOf()将其转为Long类型,但是如果转换失败,返回null;否则抛出IllegalArgumentException异常。
二、object强转list
在一些场景下,可能需要将object类型强制转换为List类型。下面来看一下如何实现这一过程。
@SuppressWarnings("unchecked")
public static <T> List<T> objectToList(Object obj) {
if (obj == null) {
return null;
}
if (!(obj instanceof List)) {
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to List");
}
return (List<T>) obj;
}
上述代码实现了将object类型强制转换为List类型的方法,如果object为null,则返回null;如果object不是List类型,则抛出IllegalArgumentException异常;否则将其强制转换为List类型并返回。
三、object转long问题
在将object类型转换为long类型时,可能会遇到一些问题,下面一一说明。
1、object转map
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> objectToMap(Object obj) {
if (obj == null) {
return null;
}
if (!(obj instanceof Map)) {
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to Map");
}
return (Map<K, V>) obj;
}
上述代码实现了将object类型转换为Map类型的方法,如果object为null,则返回null;如果object不是Map类型,则抛出IllegalArgumentException异常;否则将其强制转换为Map类型并返回。
2、object转long类型
public static Long objectToLong(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof Long) {
return (Long) obj;
}
if (obj instanceof String) {
try {
return Long.valueOf((String) obj);
} catch (NumberFormatException e) {
return null;
}
}
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to Long");
}
上述代码已在第一部分做了详细的介绍,这里不再赘述。
3、object转bean
public static <T> T objectToBean(Object obj, Class<T> clazz) {
if (obj == null) {
return null;
}
if (clazz == null) {
throw new IllegalArgumentException("clazz can not be null");
}
if (!(obj instanceof Map)) {
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to " + clazz.getName());
}
try {
T result = clazz.newInstance();
BeanUtils.populate(result, (Map<? extends String, ?>) obj);
return result;
} catch (InstantiationException e) {
throw new RuntimeException("can not instance class: " + clazz.getName(), e);
} catch (IllegalAccessException e) {
throw new RuntimeException("can not access the constructor of class: " + clazz.getName(), e);
} catch (InvocationTargetException e) {
throw new RuntimeException("can not set property of class: " + clazz.getName(), e);
}
}
上述代码实现了将object类型转换为指定类型的Bean对象,如果obj为null,则返回null;如果clazz为null,则抛出IllegalArgumentException异常;如果obj不是Map类型,则抛出IllegalArgumentException异常;否则通过BeanUtils.populate()方法将obj赋值给clazz的实例并返回。
四、object转long可能为null
在实际开发中,经常会遇到需要将object类型转换为long类型,但是存在object为null的情况,下面展示如何处理这一情况。
public static long objectToLongWithDefault(Object obj, long defaultValue) {
Long l = objectToLong(obj);
return l == null ? defaultValue : l.longValue();
}
上述代码实现了将object类型转换为long类型的方法,并且当object为null时,返回给定的默认值defaultValue。
五、object转成list
在一些场景下,需要将object类型转换为list类型,下面展示如何实现。
@SuppressWarnings("unchecked")
public static <T> List<T> objectToList(Object obj, Class<T> clazz) {
if (obj == null) {
return null;
}
if (!(obj instanceof List)) {
throw new IllegalArgumentException("can not convert " + obj.getClass().getName() + " to List");
}
List<T> result = new ArrayList<T>();
List list = (List) obj;
for (Object o : list) {
result.add((T) o);
}
return result;
}
上述代码实现了将object类型转换为List类型,并将List中的元素强制转换为给定的类型clazz。
六、小结
本文从多个方面详细阐述了object类型转换为long类型的各种情况以及如何处理,对于实际开发有很大的帮助。
原创文章,作者:WFCI,如若转载,请注明出处:https://www.506064.com/n/138576.html
微信扫一扫
支付宝扫一扫