/**
* list转map
* @param list list集合
* @param methodName 对象方法名称
* @param methodArgs 对象方法的参数
* @param <T> 对象
* @param <K> 转换Map的key数据类型
* @return Map 转换后的map
* @throws NoSuchMethodException 异常
* @throws InvocationTargetException 异常
* @throws IllegalAccessException 异常
*/
@SuppressWarnings("unchecked")
public static <K, T> Map<K, T> listToMapKey(List<T> list, String methodName, Object... methodArgs) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Map<K, T> returnMap = new HashMap<>();
if (list != null) {
for (T t : list) {
Class<?> clz = t.getClass();
Class[] argsClass = new Class[methodArgs.length];
for (int i = 0; i < methodArgs.length; i++) {
argsClass[i] = methodArgs[i].getClass();
}
Method mth = clz.getMethod(methodName, argsClass);
K key = (K) mth.invoke(t, methodArgs);
returnMap.put(key, t);
}
}
return returnMap;
}
List转Map
最新推荐文章于 2024-01-14 20:20:16 发布