I want to convert a bean class to a map (key=the name of the member,value=the value of the member).
I'm using the method BeanUtils.describe(beanClass);
( Edit :I'm using commons-beanutils 1.8.3, jdk 1.6.0_20, on commons-beanutils 1.5 it works)
The problem is that the return value is incorrect, (the map contain only the first item from the array),
the code:
public class Demo {
private ArrayList<String> myList = new ArrayList<String>();
public Demo() {
myList.add("first_value");
myList.add("second_value");
}
public ArrayList<String> getMyList() {
return myList;
}
public void setMyList(ArrayList<String> myList) {
this.myList = myList;
}
public static void main(String[] args) {
Demo myBean = new Demo();
try {
Map describe = BeanUtils.describe(myBean);
Iterator it = describe.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(String.format("key=%s,value=%s", (String) pairs.getKey(), (String) pairs.getValue()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
key=myList,value= [first_value,second_value]
key=class,value=class $Demo
key=myList,value= [first_value]
key=class,value=class $Demo
As you can see the array contains two values but the output(and the map) contains only one,why??
Thanks,
Benny
我在计算机上运行代码示例,输出为:key = myList,value = [first_value,second_value] key = class,value = class com.gpdi.infores.dao.test.Demo使用JDK5或更高版本即可。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.