List to Map

/**
* <p>将List变为Map</p>
* @param list
* @param <V> list的项目
* @param keyField 作为Map的项目
* @return 变换成的Map
* @throws Exception keyField项目没有找到的话,抛出异常
*/
private <V> Map<String, V> c(List<V> list, String keyField) throws Exception {

Map<String, V> map = new HashMap<String, V>();

for (V obj : list) {
String key = BeanUtils.getProperty(obj, keyField);
map.put(key, obj);
}
return map;
}

猜你喜欢

转载自vernonchen163.iteye.com/blog/2178150