Commit c8c7afc1 authored by yanzg's avatar yanzg

升级新版本

parent e9ac67c4
......@@ -5,23 +5,41 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 映射处理
*
* @author 颜佐光
*/
public class MapHelper {
/**
* 将一个对象列表转换为HashMap
*
* @param list 需要转换的列表
* @param <T>
* @return
* @param <T> 泛型
* @return 泛型处理
*/
public static <T extends MapKey> Map<String, T> getMap(List<T> list) {
Map<String, T> map = new HashMap<>();
if (list != null) {
public static <T extends MapKey<T>> Map<String, T> getMap(List<T> list) {
return getMap(list, (item) -> item.getKey(item));
}
/**
* 将一个对象列表转换为HashMap
*
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param <T> 泛型
* @return 泛型处理
*/
public static <T extends Object> Map<String, T> getMap(List<T> list, MapKey<T> proxy) {
if (list == null) {
return new HashMap<>(0);
}
Map<String, T> map = new HashMap<>(list.size());
for (T item : list) {
String key = item.getKey(item);
String key = proxy.getKey(item);
map.put(key, item);
}
}
return map;
}
......@@ -30,17 +48,19 @@ public class MapHelper {
*
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param <T>
* @return
* @param <T> 类型
* @param <M> 关键字类型
* @return 映射结果
*/
public static <T extends Object> Map<String, T> getMap(List<T> list, MapKey<T> proxy) {
Map<String, T> map = new HashMap<>();
if (list != null) {
public static <T, M> Map<M, T> getMapType(List<T> list, MapKeyType<T, M> proxy) {
if (list == null) {
return new HashMap<>(0);
}
Map<M, T> map = new HashMap<>(list.size());
for (T item : list) {
String key = proxy.getKey(item);
M key = proxy.getKey(item);
map.put(key, item);
}
}
return map;
}
......@@ -48,19 +68,20 @@ public class MapHelper {
* 将一个对象列表转换为HashMap
*
* @param list 需要转换的列表
* @param <T>
* @return
* @param <T> 泛型
* @return 泛型处理
*/
public static <T extends MapKey> List<String> getKeys(List<T> list) {
List<String> to = new ArrayList<>();
if (list != null) {
public static <T extends MapKey<T>> List<String> getKeys(List<T> list) {
if (list == null) {
return new ArrayList<>();
}
List<String> to = new ArrayList<>(list.size());
for (T item : list) {
String key = item.getKey(item);
if (!to.contains(key)) {
to.add(key);
}
}
}
return to;
}
......@@ -69,8 +90,8 @@ public class MapHelper {
*
* @param list 需要转换的列表
* @param proxy 获取主键的函数
* @param <T>
* @return
* @param <T> 泛型
* @return 泛型处理
*/
public static <T extends Object> List<String> getKeys(List<T> list, MapKey<T> proxy) {
List<String> to = new ArrayList<>();
......@@ -85,24 +106,15 @@ public class MapHelper {
return to;
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T>
*/
public interface MapKey<T> {
String getKey(T from);
}
/**
* 将建添加到子数组中
*
* @param mapList
* @param key
* @param item
* @param <T>
* @param <M>
* @return
* @param mapList 列表
* @param key 关键字
* @param item 对象
* @param <T> 关键字类型
* @param <M> 关键字对象
* @return 最终结果
*/
public static <T, M> List<M> addMapList(Map<T, List<M>> mapList, T key, M item) {
if (!mapList.containsKey(key)) {
......@@ -112,4 +124,28 @@ public class MapHelper {
ret.add(item);
return ret;
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T> 泛型
*/
public interface MapKey<T> extends MapKeyType<T, String> {
}
/**
* 一个获取对象关键字的处理函数
*
* @param <T> 泛型
* @param <M> 泛型
*/
public interface MapKeyType<T, M> {
/**
* 获取关键字
*
* @param from 来源数据
* @return 关键字
*/
M getKey(T from);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment