//Map转Object
public static Object mapToObject(Map<Object, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
if (map.containsKey(field.getName())) {
field.set(obj, map.get(field.getName()));
}
}
return obj;
}
//Object转Map
public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj);
if (value == null) {
value = "";
}
map.put(fieldName, value);
}
return map;
}
//Object转Map
public static Map<String, Object> getFatherObjectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj);
if (value == null) {
value = "";
}
map.put(fieldName, value);
}
return map;
}
//List转Map
public static <K, V> Map<K, V> getMapByList(List<V> list, String field) {
Map<K, V> map = new HashMap<>();
if (list == null) {
return map;
}
for (V obj : list) {
Class<?> clazz = obj.getClass();
Field f;
K fieldValue = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
fieldValue = (K) f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
map.put(fieldValue, obj);
}
return map;
}
public static <K, V> Map<K, V> getMapByFatherList(List<V> list, String field) {
Map<K, V> map = new HashMap<>();
if (list == null) {
return map;
}
for (V obj : list) {
Class<?> clazz = obj.getClass();
Field f;
K fieldValue = null;
try {
f = clazz.getField(field);
f.setAccessible(true);
fieldValue = (K) f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
map.put(fieldValue, obj);
}
return map;
}
//List转Map
public static <K, V> Map<K, List<V>> getMapByListAndGroup(List<V> list, String field) {
Map<K, List<V>> map = new HashMap<>();
if (list == null) {
return map;
}
List<V> objList;
for (V obj : list) {
Class<?> clazz = obj.getClass();
Field f;
K fieldValue = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
fieldValue = (K) f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
if (map.containsKey(fieldValue)) {
objList = map.get(fieldValue);
} else {
objList = new ArrayList<>();
}
objList.add(obj);
map.put(fieldValue, objList);
}
return map;
}
/*//List转Map
public static Map<Object, Object> getMapByList(List<?> list, String field) {
return getMapByList(list, field, false);
}
//List转Map
public static Map<Object, Object> getMapByList(List<?> list, String field, boolean isGroup) {
Map<Object, Object> map = new HashMap<>();
if (list == null) {
return map;
}
if (!isGroup) {
for (Object obj : list) {
Class<?> clazz = obj.getClass();
Field f;
Object fieldValue = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
fieldValue = f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
map.put(fieldValue, obj);
}
} else {
List<Object> objList;
for (Object obj : list) {
Class<?> clazz = obj.getClass();
Field f;
Object fieldValue = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
fieldValue = f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
if (map.containsKey(fieldValue)) {
objList = (List<Object>) map.get(fieldValue);
} else {
objList = new ArrayList<>();
}
objList.add(obj);
map.put(fieldValue, objList);
}
}
return map;
}*/
//List转Map
public static Map<Object, Object> getMapByList(List<?> list, String keyField, String valueField) {
return getMapByList(list, keyField, valueField, false);
}
//List转Map
public static Map<Object, Object> getMapByList(List<?> list, String keyField, String valueField, boolean isGroup) {
Map<Object, Object> map = new HashMap<>();
if (list == null) {
return map;
}
if (!isGroup) {
for (Object obj : list) {
Class<?> clazz = obj.getClass();
Field keyF;
Field ValeuF;
Object keyFieldValue = null;
Object valueFieldValue = null;
try {
keyF = clazz.getDeclaredField(keyField);
keyF.setAccessible(true);
keyFieldValue = keyF.get(obj);
ValeuF = clazz.getDeclaredField(valueField);
ValeuF.setAccessible(true);
valueFieldValue = ValeuF.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
map.put(keyFieldValue, valueFieldValue);
}
} else {
List<Object> valueFieldValueList;
for (Object obj : list) {
Class<?> clazz = obj.getClass();
Field keyF;
Field ValeuF;
Object keyFieldValue = null;
Object valueFieldValue = null;
try {
keyF = clazz.getDeclaredField(keyField);
keyF.setAccessible(true);
keyFieldValue = keyF.get(obj);
ValeuF = clazz.getDeclaredField(valueField);
ValeuF.setAccessible(true);
valueFieldValue = ValeuF.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
if (map.containsKey(keyFieldValue)) {
valueFieldValueList = (List<Object>) map.get(keyFieldValue);
} else {
valueFieldValueList = new ArrayList<>();
}
valueFieldValueList.add(valueFieldValue);
map.put(keyFieldValue, valueFieldValueList);
}
}
return map;
}
//覆盖添加,累加
public static void coverAdd(Map map, Object key, BigDecimal value) {
if (map.containsKey(key)) {
map.put(key, value.add((BigDecimal) map.get(key)));
} else {
map.put(key, value);
}
}