Commit 903c84f3 authored by yanzg's avatar yanzg

升级新版本

parent a087b029
...@@ -5,22 +5,40 @@ import java.util.HashMap; ...@@ -5,22 +5,40 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* 映射处理
*
* @author 颜佐光
*/
public class MapHelper { public class MapHelper {
/** /**
* 将一个对象列表转换为HashMap * 将一个对象列表转换为HashMap
* *
* @param list 需要转换的列表 * @param list 需要转换的列表
* @param <T> * @param <T> 泛型
* @return * @return 泛型处理
*/ */
public static <T extends MapKey> Map<String, T> getMap(List<T> list) { public static <T extends MapKey<T>> Map<String, T> getMap(List<T> list) {
Map<String, T> map = new HashMap<>(); return getMap(list, (item) -> item.getKey(item));
if (list != null) { }
for (T item : list) {
String key = item.getKey(item); /**
map.put(key, 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 = proxy.getKey(item);
map.put(key, item);
} }
return map; return map;
} }
...@@ -30,16 +48,18 @@ public class MapHelper { ...@@ -30,16 +48,18 @@ public class MapHelper {
* *
* @param list 需要转换的列表 * @param list 需要转换的列表
* @param proxy 获取主键的函数 * @param proxy 获取主键的函数
* @param <T> * @param <T> 类型
* @return * @param <M> 关键字类型
* @return 映射结果
*/ */
public static <T extends Object> Map<String, T> getMap(List<T> list, MapKey<T> proxy) { public static <T, M> Map<M, T> getMapType(List<T> list, MapKeyType<T, M> proxy) {
Map<String, T> map = new HashMap<>(); if (list == null) {
if (list != null) { return new HashMap<>(0);
for (T item : list) { }
String key = proxy.getKey(item); Map<M, T> map = new HashMap<>(list.size());
map.put(key, item); for (T item : list) {
} M key = proxy.getKey(item);
map.put(key, item);
} }
return map; return map;
} }
...@@ -48,17 +68,18 @@ public class MapHelper { ...@@ -48,17 +68,18 @@ public class MapHelper {
* 将一个对象列表转换为HashMap * 将一个对象列表转换为HashMap
* *
* @param list 需要转换的列表 * @param list 需要转换的列表
* @param <T> * @param <T> 泛型
* @return * @return 泛型处理
*/ */
public static <T extends MapKey> List<String> getKeys(List<T> list) { public static <T extends MapKey<T>> List<String> getKeys(List<T> list) {
List<String> to = new ArrayList<>(); if (list == null) {
if (list != null) { return new ArrayList<>();
for (T item : list) { }
String key = item.getKey(item); List<String> to = new ArrayList<>(list.size());
if (!to.contains(key)) { for (T item : list) {
to.add(key); String key = item.getKey(item);
} if (!to.contains(key)) {
to.add(key);
} }
} }
return to; return to;
...@@ -69,8 +90,8 @@ public class MapHelper { ...@@ -69,8 +90,8 @@ public class MapHelper {
* *
* @param list 需要转换的列表 * @param list 需要转换的列表
* @param proxy 获取主键的函数 * @param proxy 获取主键的函数
* @param <T> * @param <T> 泛型
* @return * @return 泛型处理
*/ */
public static <T extends Object> List<String> getKeys(List<T> list, MapKey<T> proxy) { public static <T extends Object> List<String> getKeys(List<T> list, MapKey<T> proxy) {
List<String> to = new ArrayList<>(); List<String> to = new ArrayList<>();
...@@ -85,24 +106,15 @@ public class MapHelper { ...@@ -85,24 +106,15 @@ public class MapHelper {
return to; return to;
} }
/**
* 一个获取对象关键字的处理函数
*
* @param <T>
*/
public interface MapKey<T> {
String getKey(T from);
}
/** /**
* 将建添加到子数组中 * 将建添加到子数组中
* *
* @param mapList * @param mapList 列表
* @param key * @param key 关键字
* @param item * @param item 对象
* @param <T> * @param <T> 关键字类型
* @param <M> * @param <M> 关键字对象
* @return * @return 最终结果
*/ */
public static <T, M> List<M> addMapList(Map<T, List<M>> mapList, T key, M item) { public static <T, M> List<M> addMapList(Map<T, List<M>> mapList, T key, M item) {
if (!mapList.containsKey(key)) { if (!mapList.containsKey(key)) {
...@@ -112,4 +124,28 @@ public class MapHelper { ...@@ -112,4 +124,28 @@ public class MapHelper {
ret.add(item); ret.add(item);
return ret; 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