将一个List集合按照指定属性转换成Map键值对,以指定属性为键

/**
  * 将一个List集合按照指定属性转换成Map键值对,以指定属性为键
  *
  * @param srcList
  *            源List集合
  * @param key
  *            指定属性,键
  * @return 转换所得的Map
  * @throws IntrospectionException
  *             内省期间发生异常
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  */
 public static Map transferListToMap(List srcList, String key)
   throws IntrospectionException, IllegalArgumentException,
   IllegalAccessException, InvocationTargetException {

  Map destMap = null;

  if (ValidateHelper.isNotEmptyCollection(srcList)) {
   destMap = new HashMap();
   for (Object object : srcList) {
    PropertyDescriptor pd = new PropertyDescriptor(key,
      object.getClass());
    Object keyValue = pd.getReadMethod().invoke(object);
    destMap.put(keyValue, object);
   }
  }

  return destMap;
 }

你可能感兴趣的:(Java)