定义一个操作接口
T list原生类型
K Map key类型
/**
* 作者: 李芳敏
* 日期: 2018/7/17
* 描述:
*/
public abstract class GenMapKey<T,K> {
/**
* 从T中获取mapkey
*/
public abstract K getKey(T t);
/**
* 获取MapValue
* */
public <V> V getValue(T t){
return (V) t;
}
}
import org.apache.commons.collections.CollectionUtils;
import java.util.*;
/**
* 作者: 李芳敏
* 日期: 2018/7/17
* 描述:
*/
public class MapUtil {
/**
* List bean 分类 Map 值唯一 T
*/
public static <T, K,V> Map<K, V> catogaryList(List<T> tList, GenMapKey mapEntry) {
if (CollectionUtils.isEmpty(tList)) {
return Collections.EMPTY_MAP;
}
Map<K, V> resultMap = new HashMap<>();
Iterator<T> it = tList.iterator();
T t;
V v;
K mapKey;
for (; it.hasNext(); ) {
t = it.next();
if(null == t){
continue;
}
v = (V) mapEntry.getValue(t);
mapKey = (K) mapEntry.getKey(t);
resultMap.put(mapKey, v);
}
return resultMap;
}
/**
* List bean 分类 Map 多个值 T
*/
public static <T, K,V> Map<K, List<V>> catogaryListOfMutiValue(List<T> tList, GenMapKey mapEntry) {
if (CollectionUtils.isEmpty(tList)) {
return Collections.EMPTY_MAP;
}
Map<K, List<V>> resultMap = new HashMap<>();
Iterator<T> it = tList.iterator();
T t;
V v;
K mapKey;
for (; it.hasNext(); ) {
t = it.next();
if(null == t){
continue;
}
v = (V) mapEntry.getValue(t);
mapKey = (K) mapEntry.getKey(t);
if (!resultMap.containsKey(mapKey)) {
resultMap.put(mapKey, new ArrayList<V>());
}
resultMap.get(mapKey).add(v);
}
return resultMap;
}
/**
* List bean 分类 TreeMap 值唯一 T
*/
public static <T, K,V> SortedMap<K, V> catogaryListTreeMap(List<T> tList, GenMapKey mapEntry) {
SortedMap<K, V> resultMap = new TreeMap<>();
if (CollectionUtils.isEmpty(tList)) {
return resultMap;
}
Iterator<T> it = tList.iterator();
T t;
V v;
K mapKey;
for (; it.hasNext(); ) {
t = it.next();
if (null == t) {
continue;
}
v = (V) mapEntry.getValue(t);
mapKey = (K) mapEntry.getKey(t);
resultMap.put(mapKey, v);
}
return resultMap;
}
/**
* List bean 分类 TreeMap 多个值 T
*/
public static <T, K,V> SortedMap<K, List<V>> catogaryListOfTreeMapMutiValue(List<T> tList, GenMapKey mapEntry) {
SortedMap<K, List<V>> resultMap = new TreeMap<>();
if (CollectionUtils.isEmpty(tList)) {
return resultMap;
}
Iterator<T> it = tList.iterator();
V v;
T t;
K mapKey;
for (; it.hasNext(); ) {
t = it.next();
if(null == t){
continue;
}
v = (V) mapEntry.getValue(t);
mapKey = (K) mapEntry.getKey(t);
if (!resultMap.containsKey(mapKey)) {
resultMap.put(mapKey, new ArrayList<V>());
}
resultMap.get(mapKey).add(v);
}
return resultMap;
}
}
调用方式 指定key
SortedMap<String, TriggerModelInfo> dataMap = new TreeMap<>();
Map<String, TriggerModel> modelMap = MapUtil.catogaryList(specifyConfigDao.findAllTriggerModel(), new GenMapKey<TriggerModel, String>() {
@Override
public String getKey(TriggerModel triggerModel) {
return String.valueOf(triggerModel.getId());
}
});
调用方式,指定key 指定value
//运行中广告对应的广告组
List<GroupAD> groupADs = iadAdDao.findRunAdGroup();
Map<String, List<Integer>> groupADMap = MapUtil.catogaryListOfMutiValue(
groupADs,
new GenMapKey<GroupAD, String>() {
@Override
public String getKey(GroupAD groupAD) {
return groupAD.getAdId();
}
@Override
public <V> V getValue(GroupAD groupAD) {
return (V) new Integer(groupAD.getGroupId());
}
}
);