现在有个bean:
public class Test{
    private String a;
    private String b;
    private String c;
    private String d;
    ....
}一些map:
mapA : key = a,value = 1
mapB : key = b,value = 2
mapC : key = c,value = 3如何按照 map 里的key给Test里的元素赋上对应的值呢?不用循环遍历求思路或者方法。。
(Test里的元素 map里的key 是不一定有的) 

解决方案 »

  1.   

    基本上就是用反射了。根据Map中的Key,来查找目标对象是否有set方法,然后调用进行赋值。话说用封装好的组件会更方便些。
      

  2.   

    封装好的组件可以用apache的commons-beanUtils.jar
    BeanUtils.populate(Object o,Map map);把map中的元素填充到bean中
    反射的话麻烦点:     Test t=new Test();
         for(Map.Entry entry:map.entrySet()){
             Method method=Test.class.getMethod("set"+entry.getKey(),String.class);
             method.invoke(t,entry.getValue());
         } 大致是这样