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

/**

* @Description 对象属性操作工具类

* @Package com.viathink.msswms.sample.utils.PropertiesUtils.java

* @author LiuJunGuang

* @date 2012-5-11 下午1:54:08

* @version V1.0

*/

public class PropertiesUtils {

/**

* 根据对象列表和对象的某个属性返回属性的List集合

*

* @param objList

* 对象列表

* @param propertyName

* 要操作的属性名称

* @return

 
   

* 指定属性的List集合;

* 如果objList为null或者size等于0抛出 IllegalArgumentException异常;

* 如果propertyName为null抛出 IllegalArgumentException异常

*

* @throws NoSuchMethodException

* @throws InvocationTargetException

*/

public static List getPropertyList(List 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 propList = new LinkedList();

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 Map> getPropertiesMap(List 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> maps = new HashMap>();

for (int i = 0; i < propertyName.length; i++) {

maps.put(propertyName[i], getPropertyList(objList, propertyName[i]));

}

return maps;

}

}用到BeanUtils工具包

你可能感兴趣的:(java,对象属性转list)