/** * 转换字符串到相应类型. * * @param value 待转换的字符串. * @param toType 转换目标类型. */ public static Object convertStringToObject(String value, Class<?> toType) { try { return org.apache.commons.beanutils.ConvertUtils.convert(value, toType); } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } }
/** * 提取集合中的对象的一个属性(通过Getter函数), 组合成List. * * @param collection 来源集合. * @param propertyName 要提取的属性名. */ public static List extractToList(final Collection collection, final String propertyName) { List list = new ArrayList(collection.size()); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return list; }
/** * 提取集合中的对象的属性(通过getter函数), 组合成List. * * @param collection 来源集合. * @param propertyName 要提取的属性名. */ @SuppressWarnings({"unchecked", "rawtypes"}) public static List convertElementPropertyToList( final Collection collection, final String propertyName) { List list = new ArrayList(); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return list; }
/** * 提取集合中的对象的两个属性(通过Getter函数), 组合成Map. * * @param collection 来源集合. * @param keyPropertyName 要提取为Map中的Key值的属性名. * @param valuePropertyName 要提取为Map中的Value值的属性名. */ public static Map extractToMap( final Collection collection, final String keyPropertyName, final String valuePropertyName) { Map map = new HashMap(collection.size()); try { for (Object obj : collection) { map.put( PropertyUtils.getProperty(obj, keyPropertyName), PropertyUtils.getProperty(obj, valuePropertyName)); } } catch (Exception e) { throw Reflections.convertReflectionExceptionToUnchecked(e); } return map; }