平常在工作中经常用到, List 向Map转换,
或者转换成 Map<Key, Map<Key2, Val >>,
也有转换成 Map<K, List<V>>,
方便分组提取数据, 虽然在jdk 1.8后有其它方法, 但是还是觉得工具类用起来方便, 下面就是代码实现:
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;
/**
* 将List转换为 Map的工具类
* @author guishuanglin 2020-04-20
*/
public class TestListToMap {
private static final Log logger = LogFactory.getLog(TestListToMap.class);
/**
* 将List转换为 Map<K, Map<K2, V>>
* @param dataList 需要转换的数据
* @param methodNameK 取K 值的方法名
* @param methodNameK2 取K2 值的方法名
* @return
*/
@SuppressWarnings({ "unchecked" })
public static <K,K2,V> Map<