您的位置:首页 > 编程语言 > Java开发

java将对象列表中的某个属性转换成List或Map

2017-01-03 00:00 756 查看
import org.apache.commons.beanutils.PropertyUtilsBean;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Created by AJAX on 2017/1/3.
*/
public class PropertiesUtils {
/**
* 根据对象列表和对象的某个属性返回属性的List集合
*
* @param objList
* 对象列表
* @param propertyName
* 要操作的属性名称
* @return <pre>
* 指定属性的List集合;
* 如果objList为null或者size等于0抛出 IllegalArgumentException异常;
* 如果propertyName为null抛出 IllegalArgumentException异常
* </pre>
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static <T> List<Object> getPropertyList(List<T> objList, String propertyName) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
if (objList == null || objList.size() == 0)
throw new IllegalArgumentException("No objList specified");
if (propertyName == null || "".equals(propertyName)) {
throw new IllegalArgumentException("No propertyName specified for bean class '" + objList.get(0).getClass() + "'");
}
PropertyUtilsBean p = new PropertyUtilsBean();
List<Object> propList = new LinkedList<Object>();
for (int i = 0; i < objList.size(); i++) {
T obj = objList.get(i);
propList.add(p.getProperty(obj, propertyName));
}
return propList;
}

/**
* 将List列表中的对象的某个属性封装成一个Map对象,key值是属性名,value值是对象列表中对象属性值的列表
*
* @param objList
* 对象列表
* @param propertyName
* 属性名称,可以是一个或者多个
* @return 返回封装了属性名称和属性值列表的Map对象,如果参数非法则抛出异常信息
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static <T> Map<String, List<Object>> getPropertiesMap(List<T> objList, String... propertyName)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (objList == null || objList.size() == 0)
throw new IllegalArgumentException("No objList specified");
if (propertyName == null || propertyName.length == 0) {
throw new IllegalArgumentException("No propertyName specified for bean class '" + objList.get(0).getClass() + "'");
}
Map<String, List<Object>> maps = new HashMap<String, List<Object>>();
for (int i = 0; i < propertyName.length; i++) {
maps.put(propertyName[i], getPropertyList(objList, propertyName[i]));
}
return maps;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐