```java
package org.apache.rocketmq.console.util;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* Created by kang.zou
* on 2021/9/18
*/
public class ListToMapUtil {
private static final Logger logger = LoggerFactory.getLogger(ListToMapUtil.class);
private static final String EMPTYSTR = "";
// 适用于KEY为get属性方法,VALUE为get属性方法
public static <T> Map<String, String> toMap(List<T> convertList, String getNameOfKey, String getNameOfValue) {
Map<String, String> resultMap = new HashMap<>();
if (CollectionUtils.isEmpty(convertList)) {
return resultMap;
}
for (T obj : convertList) {
if(obj == null) continue;
try{
Object key = obj.getClass().getMethod(getNameOfKey).invoke(obj);
Object value = obj.getClass().getMethod(getNameOfValue).invoke(obj);
resultMap.put(key == null ? null : String.valueOf(key), value == null ? null : String.valueOf(value));
}catch (Exception e) {
logger.error("ListToMapUtil toMap1 method is reflecting, invoke key occur error:{}", Throwables.getStackTraceAsString(e));
}
}
return resultMap;
}
// 适用于KEY为get属性方法,VALUE为get属性方法的集合, 并且KEY,VALUE都不能为空
public static <T> Map<String, List<String>> toMapListStrWithoutNull(List<T> convertList, String getNameOfKey, String getNameOfValue) {
Map<String, List<String>> resultMap = new HashMap<>();
if (CollectionUtils.isEmpty(convertList)) {
return resultMap;
}
for (T obj : convertList) {
if(obj == null) continue;
try{
Object key = obj.getClass().getMethod(getNameOfKey).invoke(obj);
Object value = obj.getClass().getMethod(getNameOfValue).invoke(obj);
if(key == null || value == null) {
continue;
}
List<String> values = resultMap.get(String.valueOf(key));
if(values == null) {
values = new ArrayList<>();
}
values.add(String.valueOf(value));
resultMap.put(String.valueOf(key), values);
}catch (Exception e) {
logger.error("ListToMapUtil toMap method is reflecting, invoke key occur error:{}", Throwables.getStackTraceAsString(e));
}
}
return resultMap;
}
// 适用于KEY为get属性方法,VALUE为单个对象, 带前缀
public static <T> Map<String, T> toMapWithPrefix(List<T> convertList, String getNameOfKey, String prefix, String separator) {
Map<String, T> resultMap = new HashMap<>();
if (CollectionUtils.isEmpty(convertList)) {
return resultMap;
}
for (T obj : convertList) {
if(obj == null) {
continue;
}
try{
Object key = obj.getClass().getMethod(getNameOfKey).invoke(obj);
resultMap.put(prefix + separator + (key == null ? null : String.valueOf(key)), obj);
}catch (Exception e) {
logger.error("ListToMapUtil toMap method is reflecting, invoke key occur error:{}", Throwables.getStackTraceAsString(e));
}
}
return resultMap;
}
// 适用于KEY为get属性方法,VALUE为单个对象, 带前缀+分割符
public static <T> Map<String, T> toMapWithPrefix(List<T> convertList, String getNameOfKey, String prefix) {
return toMapWithPrefix(convertList, getNameOfKey, prefix, EMPTYSTR);
}
// 适用于KEY为get属性方法,VALUE为单个对象
public static <T> Map<String, T> toMap(List<T> convertList, String getNameOfKey) {
return toMapWithPrefix(convertList, getNameOfKey, EMPTYSTR, EMPTYSTR);
}
// 适用于KEY为get属性方法,VALUE为对象的集合
public static <T> Map<String, List<T>> toMapList(List<T> convertList, String getNameOfKey) {
Map<String, List<T>> resultMap = new HashMap<>();
if (CollectionUtils.isEmpty(convertList)) {
return resultMap;
}
for (T obj : convertList) {
if(obj == null) {
continue;
}
try{
Object keyObj = obj.getClass().getMethod(getNameOfKey).invoke(obj);
String key = keyObj == null ? null : String.valueOf(keyObj);
List<T> list = resultMap.get(key);
if(list == null) {
list = new ArrayList<>();
}
list.add(obj);
resultMap.put(key, list);
}catch (Exception e) {
logger.error("ListToMapUtil toMap method is reflecting, invoke key occur error:{}", Throwables.getStackTraceAsString(e));
}
}
return resultMap;
}
public static void main(String[] args) {
List<Test> convertList = Lists.newArrayList(new Test("pdd", 23, null, "guichu"), new Test("lbw", 24,
new Date(), "running"), new Test(null, 21, null, "chess"), new Test("pdd", 23, null, "game"));
System.out.println(ListToMapUtil.toMap(convertList, "getId", "getHabit"));
System.out.println(ListToMapUtil.toMapListStrWithoutNull(convertList, "getId", "getHabit"));
System.out.println(ListToMapUtil.toMap(convertList, "getId"));
System.out.println(ListToMapUtil.toMapList(convertList, "getId"));
System.out.println(ListToMapUtil.toMapWithPrefix(convertList, "getId", "SUB"));
System.out.println(ListToMapUtil.toMapWithPrefix(convertList, "getId", "SUB", "|"));
}
public static class Test {
private String id;
private int age;
private Date birth;
private String habit;
public Test(){}
public Test(String id, int age, Date birth, String habit) {
this.id = id;
this.age = age;
this.birth = birth;
this.habit = habit;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Test{" +
"id='" + id + '\'' +
", age=" + age +
", birth=" + birth +
", habit='" + habit + '\'' +
'}';
}
public String getHabit() {
return habit;
}
public void setHabit(String habit) {
this.habit = habit;
}
}
}
|