List轉(zhuǎn)換成Map工具類的簡單實例
更新時間:2017年01月20日 08:56:46 投稿:jingxian
下面小編就為大家?guī)硪黄狶ist轉(zhuǎn)換成Map工具類的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
public class List2MapUtils {
/**
* K: key class type, V: value class type
*
* @param sourceList
* @param keyName
* key property
* @param keyClass
* key Class type
* @return
*/
public static <K, V> Map<K, V> convert2Map(List<V> sourceList, String keyName, Class<K> keyClass) {
Map<K, V> map = new HashMap<K, V>();
if (sourceList == null || sourceList.isEmpty()) {
return map;
}
for (V value : sourceList) {
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(value);
beanWrapper.setAutoGrowNestedPaths(true);
K key = keyClass.cast(beanWrapper.getPropertyValue(keyName));
if (key == null) {
continue;
}
map.put(key, value);
}
return map;
}
}
以上這篇List轉(zhuǎn)換成Map工具類的簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中@RequiredArgsConstructor注解的基本用法
這篇文章主要介紹了Java中@RequiredArgsConstructor注解的基本用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
spring boot tomcat jdbc pool的屬性綁定
這篇文章主要介紹了spring boot tomcat jdbc pool的屬性綁定的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友參考下2018-01-01
java實現(xiàn)順序結(jié)構(gòu)線性列表的函數(shù)代碼
java實現(xiàn)順序結(jié)構(gòu)線性列表的函數(shù)代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10

