import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;
/**
*
* 实体对象工具类
*
*
*/
public class EntityUtil {
/**
*
* 将list中的元素放到Map<K, V>以建立 key - value 索引<p>
*
* @param list List<V> 元素列表
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
* @param <K> key类型
* @param <V> value类型
* @return Map<K, V> key - value 索引
*
*
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> makeEntityMap(List<V> list, String keyFieldName) {
Map<K, V> map = new HashMap<>();
if(list == null || list.size() == 0) {
return map;
}
try {
Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
for (V item : list) {
map.put((K) getter.invoke(item), item);
}
} catch (Exception e) {
logger.error("makeEntityMap error list is " + list, e);
return map;
}
return map;
}
/**
*
* 将list中的元素放到Map<String, V>以建立 key - value 索引<p>
*
* @param list List<V> 元素列表
* @param splitVar 属性之间间隔
* @param keyFieldNames String 元素的属性名称动态数组, 依次循环该属性的值作为索引key
* @param <V> value类型
* @return Map<String, V> key - value 索引
*
*
*/
@SuppressWarnings("unchecked")
public static <V> Map<String, V> makeEntityMapByKeys(List<V> list,String splitVar , String... keyFieldNames) {
Map<String, V> map = new HashMap<>();
if(list == null || list.size() == 0 || keyFieldNames == null || keyFieldNames.length==0 || StringUtil.isStringEmpty(splitVar)) {
return map;
}
try {
List<Method> getterList = new ArrayList<>();
for(String key : keyFieldNames){
getterList.add(getMethod(list.get(0).getClass(),key, "get"));
}
for (V item : list) {
StringBuffer keys= new StringBuffer("");
for (int i=0;i<getterList.size();i++){
keys.append(getterList.get(i).invoke(item));
if(i<getterList.size()-1){
keys.append(splitVar);
}
}
map.put(keys.toString(), item);
}
} catch (Exception e) {
logger.error("makeEntityMap error list is " + list, e);
return map;
}
return map;
}
/**
*
* 将list中的元素放到Map<K, List<V>> 以建立 key - List<value> 索引<p>
*
* @param list List<V> 元素列表
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
* @param <K> key类型
* @param <V> value类型
* @return Map<K, V> key - value 索引
*
*
*/
public static <K, V> Map<K, List<V>> makeEntityListMap(List<V> list, String keyFieldName) {
Map<K, List<V>> map = new LinkedHashMap<>();
if(list == null || list.size() == 0) {
return map;
}
try {
Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
for (V item : list) {
@SuppressWarnings("unchecked")
K key = (K) getter.invoke(item);
List<V> groupList = map.get(key);
if (groupList == null) {
groupList = new ArrayList<>();
map.put(key, groupList);
}
groupList.add(item);
}
} catch (Exception e) {
logger.error("makeEntityListMap error list is " + list, e);
return map;
}
return map;
}
/**
* 获取getter或setter
*/
@SuppressWarnings("unchecked")
private static Method getMethod(@SuppressWarnings("rawtypes") Class clazz, String fieldName,
String methodPrefix) throws NoSuchMethodException {
String first = fieldName.substring(0, 1);
String getterName = methodPrefix + fieldName.replaceFirst(first, first.toUpperCase());
return clazz.getMethod(getterName);
}
/**
* 比较两个对象的值是否不同
* @param obj1 对象1
* @param obj2 对象2
* @param <T> object
* @return 若俩对象的值不相同则为true,反之为false
*/
public static <T> Boolean findDifference(T obj1,T obj2){
if(obj1 == null && obj2 == null){
return false;
}
if(obj1 == null || obj2 == null){
return true;
}
if(obj1 instanceof BigDecimal){
return ((BigDecimal) obj1).compareTo((BigDecimal)obj2) != 0;
}else {
return !obj1.equals(obj2);
}
}
}
import java.math.BigDecimal;
import java.util.*;
/**
*
* 实体对象工具类
*
*
*/
public class EntityUtil {
/**
*
* 将list中的元素放到Map<K, V>以建立 key - value 索引<p>
*
* @param list List<V> 元素列表
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
* @param <K> key类型
* @param <V> value类型
* @return Map<K, V> key - value 索引
*
*
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> makeEntityMap(List<V> list, String keyFieldName) {
Map<K, V> map = new HashMap<>();
if(list == null || list.size() == 0) {
return map;
}
try {
Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
for (V item : list) {
map.put((K) getter.invoke(item), item);
}
} catch (Exception e) {
logger.error("makeEntityMap error list is " + list, e);
return map;
}
return map;
}
/**
*
* 将list中的元素放到Map<String, V>以建立 key - value 索引<p>
*
* @param list List<V> 元素列表
* @param splitVar 属性之间间隔
* @param keyFieldNames String 元素的属性名称动态数组, 依次循环该属性的值作为索引key
* @param <V> value类型
* @return Map<String, V> key - value 索引
*
*
*/
@SuppressWarnings("unchecked")
public static <V> Map<String, V> makeEntityMapByKeys(List<V> list,String splitVar , String... keyFieldNames) {
Map<String, V> map = new HashMap<>();
if(list == null || list.size() == 0 || keyFieldNames == null || keyFieldNames.length==0 || StringUtil.isStringEmpty(splitVar)) {
return map;
}
try {
List<Method> getterList = new ArrayList<>();
for(String key : keyFieldNames){
getterList.add(getMethod(list.get(0).getClass(),key, "get"));
}
for (V item : list) {
StringBuffer keys= new StringBuffer("");
for (int i=0;i<getterList.size();i++){
keys.append(getterList.get(i).invoke(item));
if(i<getterList.size()-1){
keys.append(splitVar);
}
}
map.put(keys.toString(), item);
}
} catch (Exception e) {
logger.error("makeEntityMap error list is " + list, e);
return map;
}
return map;
}
/**
*
* 将list中的元素放到Map<K, List<V>> 以建立 key - List<value> 索引<p>
*
* @param list List<V> 元素列表
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
* @param <K> key类型
* @param <V> value类型
* @return Map<K, V> key - value 索引
*
*
*/
public static <K, V> Map<K, List<V>> makeEntityListMap(List<V> list, String keyFieldName) {
Map<K, List<V>> map = new LinkedHashMap<>();
if(list == null || list.size() == 0) {
return map;
}
try {
Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
for (V item : list) {
@SuppressWarnings("unchecked")
K key = (K) getter.invoke(item);
List<V> groupList = map.get(key);
if (groupList == null) {
groupList = new ArrayList<>();
map.put(key, groupList);
}
groupList.add(item);
}
} catch (Exception e) {
logger.error("makeEntityListMap error list is " + list, e);
return map;
}
return map;
}
/**
* 获取getter或setter
*/
@SuppressWarnings("unchecked")
private static Method getMethod(@SuppressWarnings("rawtypes") Class clazz, String fieldName,
String methodPrefix) throws NoSuchMethodException {
String first = fieldName.substring(0, 1);
String getterName = methodPrefix + fieldName.replaceFirst(first, first.toUpperCase());
return clazz.getMethod(getterName);
}
/**
* 比较两个对象的值是否不同
* @param obj1 对象1
* @param obj2 对象2
* @param <T> object
* @return 若俩对象的值不相同则为true,反之为false
*/
public static <T> Boolean findDifference(T obj1,T obj2){
if(obj1 == null && obj2 == null){
return false;
}
if(obj1 == null || obj2 == null){
return true;
}
if(obj1 instanceof BigDecimal){
return ((BigDecimal) obj1).compareTo((BigDecimal)obj2) != 0;
}else {
return !obj1.equals(obj2);
}
}
}