public static<T> List<Object> listToList(Collection<T> list,String fieldName) throws Exception {
List<Object> ret = new ArrayList();
List<String>getStrs=null;
List<Method> getMethods=new ArrayList<Method>();
for(T t:list){
if(getStrs==null){
getStrs = new ArrayList<String>();
for(String s:fieldName.split("\\.")){
getStrs.add("get"+s.substring(0,1).toUpperCase()+s.substring(1));
}
}
Object value = t;
for(int i=0;i<getStrs.size();i++){
if(getMethods==null || getMethods.size()<=i ||getMethods.get(i)==null){
getMethods.add(value.getClass().getDeclaredMethod(getStrs.get(i)));
}
value=getMethods.get(i).invoke(value);
}
ret.add(value);
}
return ret;
}
java利用反射机制获取list中的某个字段并以list形式返回
猜你喜欢
转载自www.cnblogs.com/YuyuanNo1/p/9237161.html
今日推荐
周排行