读书人

List变换为Map

发布时间: 2012-09-10 11:02:32 作者: rapoo

List转换为Map

public static <K, E> Map<K, E> convert2(Collection<E> col, String propertyName)
{
Map<K, E> map = new HashMap<K, E>();

if(null == col){
return map;
}
for (E obj : col) {
BeanMap bm = new BeanMap(obj);
K value = (K)bm.get(propertyName);
map.put(value, obj);
}
return map;
}

调用

List<DsmallCategory> normalList = null;
Map<Integer, DsmallCategory> normal = Util.<Integer, DsmallCategory> convert2(normalList, "cateId");

读书人网 >编程

热点推荐