`

List转换为Map

    博客分类:
  • J2SE
阅读更多

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");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics