使用Dozer将选定的键从HashMap映射到HashMap

时间:2017-11-21 20:51:45

标签: java dozer

我的目标是将特定键从HashMap映射到HashMap。我以为那是关键'属性会帮助我。但我无法以预期的方式使其发挥作用。

public class A {
  private Map<String, String> map = new HashMap<>();

  public A() {
    map.put("1", "a");
    map.put("2", "b");
    map.put("3", "c");
  }

  public Map<String, String> getMap() {
    return map;
  }

  public void setMap(Map<String, String> map) {
    this.map = map;
  }
}

public class B {
  private Map<String, String> map = new HashMap<String, String>();

  public B() {}

  public Map<String, String> getMap() {
    return map;
  }

  public void setMap(Map<String, String> map) {
    this.map = map;
  }
}

mapping.xml:
<mapping wildcard="false">
  <class-a>test.A</class-a>
  <class-b>test.B</class-b>
  <field>
    <a key="2">map</a>
    <b key="2">map</b>
  </field>
</mapping>

public static void main(String... args) {
  DozerBeanMapper mapper = new DozerBeanMapper(...);
  A a = new A();
  B b = mapper.map(a, B.class);
  System.out.println(b.getMap());
}

结果是{1 = a,2 = b,3 = c},但我只想要{2 = b}。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

&#39;地图取得-方法&#39;和&#39; map-set-method&#39;属性起了作用:

<mapping wildcard="false">
  <class-a>test.A</class-a>
  <class-b>test.B</class-b>
  <field>
    <a map-get-method="get" key="2">map</a>
    <b map-set-method="put" key="2">map</b>
  </field>
</mapping>