我有一个列表的列表,想用Dozer和自定义转换器将其映射成一个二维数组[][]。
转换器仅用于在
我查看了以下问题的答案,但如果要处理数组而不是Set/List,则应该如何处理自定义转换器? 从HashSet到ArrayList的Dozer映射 任何建议将不胜感激。 谢谢
public class Field {
List<String> items;
public void add(String s) {
items.add(s);
}
}
public class ClassA {
int anotherVariable;
List<Field> fields;
public void add(Field f) {
fields.add(f);
}
}
public class ClassB {
int anotherVariable;
String[][] itemValues;
}
@Test
public void convertListTo2DArray() {
Field field1 = new Field();
field1.add("m"); field1.add("n");
Field field2 = new Field();
field2.add("o"); field2.add("p");
ClassA classA = new ClassA();
classA.add(field1);
classA.add(field2);
classA.setAnotherVariable(99);
List<Converter> converters = new ArrayList<Converter>();
converters.add(new ListToArrayConverter());
ClassB classB = new DozerBeanMapper().setCustomConverters(converters).map(classA, ClassB.class);
/**
* Result:
* classB -> anotherVariable = 99
*
* classB -> itemValues[][] =
* ["m", "n"]
* ["o", "p"]
*/
}
转换器仅用于在
List<List>
和String[][]
之间进行转换,不用于其他变量。我查看了以下问题的答案,但如果要处理数组而不是Set/List,则应该如何处理自定义转换器? 从HashSet到ArrayList的Dozer映射 任何建议将不胜感激。 谢谢