使用 Dozer 仅将选定的键从 HashMap 映射到 HashMap
Map only selected keys from HashMap to HashMap with Dozer
我的目标是将特定键从 HashMap 映射到 HashMap。我认为 'key' 属性会对我有所帮助。但是我无法让它以预期的方式工作。
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}。我怎样才能做到这一点?
'map-get-method' 和 'map-set-method' 属性起到了作用:
<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>
我的目标是将特定键从 HashMap 映射到 HashMap。我认为 'key' 属性会对我有所帮助。但是我无法让它以预期的方式工作。
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}。我怎样才能做到这一点?
'map-get-method' 和 'map-set-method' 属性起到了作用:
<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>