|
@@ -6,6 +6,7 @@ import java.beans.Introspector;
|
|
|
import java.beans.PropertyDescriptor;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -135,17 +136,40 @@ public class MapUtil {
|
|
|
* @return java.util.Map
|
|
|
* @describe mybatis返回结果转换为Map
|
|
|
*/
|
|
|
- public static <K, V> Map<K, V> convertIntegerMap(List<Map<K, V>> maps) {
|
|
|
+ public static <K, V> Map<K, V> convertIntegerMap(List<Map<K, V>> maps,Class keyType,Class valueType) {
|
|
|
int size = 0;
|
|
|
- if (maps != null) {
|
|
|
+ if(maps != null){
|
|
|
size = maps.size();
|
|
|
}
|
|
|
- Map<K, V> result = new HashMap<K, V>(size);
|
|
|
-
|
|
|
- for (Map<K, V> map : maps) {
|
|
|
- for (Entry<K, V> entry : map.entrySet()) {
|
|
|
- result.put(entry.getKey(), entry.getValue());
|
|
|
- }
|
|
|
+ Map result=new HashMap(size);
|
|
|
+ try {
|
|
|
+ for (Map<K, V> stringIntegerMap : maps) {
|
|
|
+ if(Objects.isNull(stringIntegerMap)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(stringIntegerMap.get("value") == null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ K key;
|
|
|
+ V value;
|
|
|
+ if(keyType.isAssignableFrom(BigDecimal.class)){
|
|
|
+ key = (K) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(stringIntegerMap.get("key").toString());
|
|
|
+ }else if(keyType.isAssignableFrom(String.class)){
|
|
|
+ key = (K) stringIntegerMap.get("key").toString();
|
|
|
+ }else{
|
|
|
+ key = (K) keyType.getMethod("valueOf", String.class).invoke(null,stringIntegerMap.get("key").toString());
|
|
|
+ }
|
|
|
+ if(valueType.isAssignableFrom(BigDecimal.class)){
|
|
|
+ value = (V) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(stringIntegerMap.get("value").toString());
|
|
|
+ }else if(valueType.isAssignableFrom(String.class)){
|
|
|
+ value = (V) stringIntegerMap.get("value").toString();
|
|
|
+ }else{
|
|
|
+ value = (V) valueType.getMethod("valueOf", String.class).invoke(null,stringIntegerMap.get("value").toString());
|
|
|
+ }
|
|
|
+ result.put(key, value);
|
|
|
+ };
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
return result;
|
|
|
}
|