一、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/zh-tw/n/138576.html