本文共 4303 字,大约阅读时间需要 14 分钟。
平常在工作中经常用到, 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> * @param dataList 需要转换的数据 * @param methodNameK 取K 值的方法名 * @param methodNameK2 取K2 值的方法名 * @return */ @SuppressWarnings({ "unchecked" }) public static Map > ListToMapMap(List dataList, String methodNameK, String methodNameK2) { if (CollectionUtils.isEmpty(dataList)) { return null; } if (methodNameK == null || "".equals(methodNameK)) { logger.error("param methodNameK is null"); return null; } if (methodNameK2 == null || "".equals(methodNameK2)) { logger.error("param methodNameK2 is null"); return null; } Method m = null; Method m2 = null; V dataObj = dataList.get(0); try { m = dataObj.getClass().getDeclaredMethod(methodNameK); m2 = dataObj.getClass().getDeclaredMethod(methodNameK2); } catch (NoSuchMethodException e) { logger.error("ListToMapMap error", e); } Map > dataMap = new HashMap >(); for (V data : dataList) { if(data != null){ try { K k = (K)m.invoke(data, new Object[]{}); K2 k2 = (K2)m2.invoke(data, new Object[]{}); Map temp = null; if (dataMap.containsKey(k)) { temp = dataMap.get(k); } else { temp = new HashMap (); dataMap.put(k, temp); } temp.put(k2, data); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } return dataMap; } /** * 将List转换为 Map > * @param dataList 需要转换的数据 * @param methodNameK 取K 值的方法名 * @return */ @SuppressWarnings({ "unchecked" }) public static Map > ListToMapList(List dataList, String methodNameK) { if (CollectionUtils.isEmpty(dataList)) { return null; } if (methodNameK == null || "".equals(methodNameK)) { logger.error("param methodNameK is null"); return null; } Method method = null; V dataObj = dataList.get(0); try { method = dataObj.getClass().getDeclaredMethod(methodNameK); } catch (NoSuchMethodException e) { logger.error("ListToMapList error", e); } Map > dataMap = new HashMap >(); for (V data : dataList) { if(data != null){ try { K k = (K)method.invoke(data, new Object[]{}); List temp = null; if (dataMap.containsKey(k)) { temp = dataMap.get(k); } else { temp = new ArrayList (); dataMap.put(k, temp); } temp.add(data); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } return dataMap; } /** * 将List转换为map * @param dataList 需要转换的数据 * @param methodNameK 取K 值的方法名 * @return */ @SuppressWarnings({ "unchecked" }) public static Map ListToMap(List dataList, String methodNameK) { if (CollectionUtils.isEmpty(dataList)) { return null; } if (methodNameK == null || "".equals(methodNameK)) { logger.error("param methodNameKK is null"); return null; } Method method = null; V dataObj = dataList.get(0); try { method = dataObj.getClass().getDeclaredMethod(methodNameK); } catch (NoSuchMethodException e) { logger.error("ListToMapList error", e); } Map dataMap = new HashMap (); for (V data : dataList) { if(data != null){ try { K k = (K)method.invoke(data, new Object[]{}); dataMap.put(k, data); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } return dataMap; } public static void main(String[] args) { //自己可以写测试代码 }}
测试代码就不上了, 上面的类是完整的, 如果没有 springframework 包, 可以自己用代码判断是不是空List,
logging 包也不是必须的, 如果没有可以用java自己的输出.
转载地址:http://sdnmd.baihongyu.com/