`

List实用类

 
阅读更多
/**
* List实用类
*
* @author vernon.chen
*
*/
public class ListUtils extends org.apache.commons.collections.ListUtils {

/**
* <p>
* 将List变为Map
* </p>
*
* @param list
* @param <V>
*            list的项目
* @param keyField
*            作为Map的项目
* @return 变换成的Map
* @throws Exception
*             keyField项目没有找到的话,抛出异常
*/
public static <V> Map<String, V> listToMap(
List<V> list,
String keyField)
{
Map<String, V> map = new HashMap<String, V>();
for (V obj : list) {
String key = null;
try {
key = BeanUtils.getProperty(obj, keyField);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
map.put(key, obj);
}
return map;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics