平常在工作中經常用到, 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<K, Map<K2, V>> ListToMapMap(List<V> 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<K, Map<K2,V>> dataMap = new HashMap<K, Map<K2,V>>();
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<K2,V> temp = null;
if (dataMap.containsKey(k)) {
temp = dataMap.get(k);
} else {
temp = new HashMap<K2,V>();
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<K, List<V>>
* @param dataList 需要轉換的數據
* @param methodNameK 取K 值的方法名
* @return
*/
@SuppressWarnings({ "unchecked" })
public static <K, V> Map<K, List<V>> ListToMapList(List<V> 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<K, List<V>> dataMap = new HashMap<K, List<V>>();
for (V data : dataList) {
if(data != null){
try {
K k = (K)method.invoke(data, new Object[]{});
List<V> temp = null;
if (dataMap.containsKey(k)) {
temp = dataMap.get(k);
} else {
temp = new ArrayList<V>();
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 <K, V> Map<K, V> ListToMap(List<V> 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<K, V> dataMap = new HashMap<K, V>();
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自己的輸出.