public class ListUtils {
/**
* 判断list中存储的对象列表中是否存在一个某属性与指定值相匹配的元素
*
* @param objList 对象列表
* @param propertyName 指定的属性
* @param destObj 要匹配的值
* @return boolean
*/
public static boolean contains(List objList, String propertyName, Object destObj) {
boolean exist = false;
Iterator it = objList.iterator();
while (it.hasNext()) {
Object bean = it.next();
try {
String getMethodStr = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
Object propertyValue = getMethod.invoke(bean, null);
if (propertyValue.equals(destObj)) {
exist = true;
break;
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return exist;
}
/**
* 判断list中存储的对象列表中是否存在一个某组属性与指定一组值相匹配的元素
*
* @param objList 对象列表
* @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
* 指定的属性数组
* @param destObjs 要匹配的值数组
* @return boolean
*/
public static boolean contains(List objList, String[] propertyNames, Object[] destObjs) {
boolean exist = false;
Iterator it = objList.iterator();
while (it.hasNext()) {
try {
Object bean = it.next();
int i = 0;
for (i = 0; i < propertyNames.length; i++) {
String getMethodStr = "get" + Character.toUpperCase(propertyNames[i].charAt(0)) + propertyNames[i].substring(1);
Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
Object propertyValue = getMethod.invoke(bean, null);
if (!propertyValue.equals(destObjs[i])) {
break;
}
}
if (i == propertyNames.length) {
exist = true;
break;
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return exist;
}
/**
* 获取list存储的对象列表中与某组属性与指定一组值相匹配的元素
*
* @param objList 对象列表
* @param propertyName (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
* 指定的属性
* @param destObj 要匹配的值
* @return Object
*/
public static Object getMatchedBean(List objList, String propertyName, Object destObj) {
Object matchBean = null;
Iterator it = objList.iterator();
while (it.hasNext()) {
try {
Object bean = it.next();
String getMethodStr = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
Object propertyValue = getMethod.invoke(bean, null);
if (propertyValue.equals(destObj)) {
matchBean = bean;
break;
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return matchBean;
}
/**
* 获取list存储的对象列表中与某组属性与指定一组值相匹配的元素
*
* @param objList 对象列表
* @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
* 指定的属性数组
* @param destObjs 要匹配的值数组
* @return Object
*/
public static Object getMatchedBean(List objList, String[] propertyNames, Object[] destObjs) {
Object matchBean = null;
Iterator it = objList.iterator();
while (it.hasNext()) {
try {
Object bean = it.next();
int i = 0;
for (i = 0; i < propertyNames.length; i++) {
String getMethodStr = "get" + Character.toUpperCase(propertyNames[i].charAt(0)) + propertyNames[i].substring(1);
Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
Object propertyValue = getMethod.invoke(bean, null);
if (!propertyValue.equals(destObjs[i])) {
break;
}
}
if (i == propertyNames.length) {
matchBean = bean;
break;
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return matchBean;
}
/**
* 移除list对象列表中与某组属性与指定一组值相匹配的元素
*
* @param objList 对象列表
* @param propertyName (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
* 指定的属性
* @param destObj 要匹配的值
*/
public static void removeMatchedBean(List objList, String propertyName, Object destObj) {
Iterator it = objList.iterator();
while (it.hasNext()) {
try {
Object bean = it.next();
PropertyDescriptor pd = new PropertyDescriptor(propertyName, bean.getClass());
Method readMethod = pd.getReadMethod();
Object propertyValue = readMethod.invoke(bean, null);
if (propertyValue.equals(destObj)) {
it.remove();
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
/**
* 移除list对象列表中与某组属性与指定一组值相匹配的元素
*
* @param objList 对象列表
* @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
* 指定的属性数组
* @param destObjs 要匹配的值数组
*/
public static void removeMatchedBean(List objList, String[] propertyNames, Object[] destObjs) {
Iterator it = objList.iterator();
while (it.hasNext()) {
try {
Object bean = it.next();
int i = 0;
for (i = 0; i < propertyNames.length; i++) {
PropertyDescriptor pd = new PropertyDescriptor(propertyNames[i], bean.getClass());
Method readMethod = pd.getReadMethod();
Object propertyValue = readMethod.invoke(bean, null);
if (!propertyValue.equals(destObjs[i])) {
break;
}
}
if (i == propertyNames.length) {
it.remove();
}
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
/**
* 判断list是否是空的
*
* @param list 要判断的目标list
* @return boolean
*/
public static boolean isNotBlankList(List list) {
boolean isNotBlank = true;
if (list != null && list.size() > 0) {
isNotBlank = true;
} else {
isNotBlank = false;
}
return isNotBlank;
}
/**
* 将数组转换成List
*
* @param strs
* @return
*/
public static List<String> toStringList(String[] strs) {
List<String> returnLst = new ArrayList<String>(strs.length);
for (String objstr : strs) {
returnLst.add(objstr);
}
return returnLst;
}
/**
* 将数组转换成List
*
* @param strs :以,分隔
* @return
*/
public static List<String> toStringList(String str) {
List<String> returnLst = new ArrayList<String>();
if (StringUtils.isBlank(str)) {
return returnLst;
}
String[] strs = str.split(",");
for (String objstr : strs) {
if (StringUtils.isNotBlank(objstr)) {
returnLst.add(objstr);
}
}
return returnLst;
}
/**
* 将字符串转换成Set
*
* @param strs :以,分隔
* @return
*/
public static Set<String> toStringSet(String str) {
Set<String> returnSet = new HashSet<String>();
if (StringUtils.isBlank(str)) {
return returnSet;
}
String[] strs = str.split(",");
for (String objstr : strs) {
if (StringUtils.isNotBlank(objstr)) {
returnSet.add(objstr);
}
}
return returnSet;
}
public static <T> Set<T> listToSet(List<T> listt){
Set<T> returnSet = new HashSet<T>();
if(null!=listt&&!listt.isEmpty()){
for(T t: listt){
returnSet.add(t);
}
}
return returnSet;
}
/**
* @param ids
* @return
* @author:Administrator
*/
public static List<Long> toLongList(String[] strs) {
List<Long> returnLst = new ArrayList<Long>(strs.length);
for (String objstr : strs) {
returnLst.add(Long.parseLong(objstr));
}
return returnLst;
}
}
java 对list的操作
最新推荐文章于 2024-08-23 00:40:55 发布