将java对象树转换为二维表

将java对象树转换为二维表,java,tree,transformation,product,cartesian,Java,Tree,Transformation,Product,Cartesian,我面临以下问题。我有一个java对象树,我必须将每个字段值导出到CSV文件中。导出的结果必须类似于我们在SQL左外部联接(称为笛卡尔积)中得到的结果 类作者 @DataField(pos = 1) String firstName; @DataField(pos = 2) String lastName; @OneToMany List<Book> books; @OneToMany List<Editor> editors; @DataField(pos = 7

我面临以下问题。我有一个java对象树,我必须将每个字段值导出到CSV文件中。导出的结果必须类似于我们在SQL左外部联接(称为笛卡尔积)中得到的结果

类作者

@DataField(pos = 1)
String firstName;

@DataField(pos = 2)
String lastName;

@OneToMany
List<Book> books;

@OneToMany
List<Editor> editors;

@DataField(pos = 7)
String Age;
类编辑器

@DataField(pos = 8)
String name;
备注: -@DataField annotation表示值在CSV记录中的位置 -在本例中,我们有一个对象作者(Charles,Moulliard),其中包含两本书的列表(“行动中的骆驼”和“行动中的骆驼2”)。第一本书有三个参考书目(ISBN 1234、ISBN 5678、ISBN 999)和第二本参考书目(ISBB 1111)。作者还包括两位编辑的名单(“曼宁”、“曼宁2”)

下面是一个示例和所需的结果

“名”、“姓”、“年龄”、“职称”、“年份”、“类型”、“代码”、“姓名” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“1234”,“曼宁”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“1234”,“曼宁2”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“5678”,“曼宁”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“5678”,“曼宁2”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“9999”,“曼宁”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼”,“2009”,“ISBN”,“9999”,“曼宁2”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼2”,“2011”,“ISBB”,“1111”,“曼宁”,“43” “查尔斯”,“莫利亚德”,“行动中的骆驼2”,“2011”,“ISBB”,“1111”,“曼宁2”,“43”

我曾尝试使用递归函数将字段值放入LinkedList:Map的Map中,其中Integer=字段在CSV中的位置,LinkedList=对象列表,但我丢失了有关元素在树中位置的信息

问候,


查尔斯我可能不明白你的问题。像这样的东西不管用吗

ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
for (Author author:authors) {
    for (Book book:author.getBooks()) {
        for (Reference reference:book.getReferences()){
            for (Editor editor:editors) {
                table.add(new ArrayList<String>(Arrays.toList({author.getFirstName(), 
                                            author.getLastName(), 
                                            book.getTitle(), 
                                            book.getYear(), 
                                            reference.getType(), 
                                            reference.getCode(), 
                                            editor.getName()})
                                    );
                );
            }
        }
    }
}
ArrayList table=new ArrayList();
for(作者:作者){
for(Book:author.getBooks()){
for(参考:book.getReferences()){
for(编辑:编辑){
table.add(新的ArrayList(Arrays.toList({author.getFirstName(),
author.getLastName(),
book.getTitle(),
book.getYear(),
reference.getType(),
reference.getCode(),
editor.getName()})
);
);
}
}
}
}

我可能不理解你的问题。像这样的东西不管用吗

ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
for (Author author:authors) {
    for (Book book:author.getBooks()) {
        for (Reference reference:book.getReferences()){
            for (Editor editor:editors) {
                table.add(new ArrayList<String>(Arrays.toList({author.getFirstName(), 
                                            author.getLastName(), 
                                            book.getTitle(), 
                                            book.getYear(), 
                                            reference.getType(), 
                                            reference.getCode(), 
                                            editor.getName()})
                                    );
                );
            }
        }
    }
}
ArrayList table=new ArrayList();
for(作者:作者){
for(Book:author.getBooks()){
for(参考:book.getReferences()){
for(编辑:编辑){
table.add(新的ArrayList(Arrays.toList({author.getFirstName(),
author.getLastName(),
book.getTitle(),
book.getYear(),
reference.getType(),
reference.getCode(),
editor.getName()})
);
);
}
}
}
}
映射值=新的HashMap();
value.put(1,Arrays.asList(“Charles”);
value.put(2,Arrays.asList(“Moulliard”);
values.put(3,array.asList(“Camel-in-Action”,“Camel-in-Action 2”);
value.put(4,Arrays.asList(“2009”、“2011”));
value.put(5,Arrays.asList(“ISBN”、“ISBN”、“ISBN”));
value.put(6,Arrays.asList(“1234”、“9876”、“7777”);
对于(列表l:s.产品(价值)){
系统输出打印LN(l);
}
}
公共列表产品(映射值){
列表产品=新的ArrayList();
映射索引=新的HashMap();
布尔incIndex=false;
而(!incIndex){
incIndex=真;
列表v=新的ArrayList();
for(int i=1;i++){
列表l=值。获取(i);
if(l==null){
打破
}
int-idx=0;
if(索引containsKey(i)){
idx=索引.get(i);
}
v、 添加(l.get(idx));
if(incIndex){
如果(++idx>=l.size()){
idx=0;
}否则{
incIndex=假;
}
指数put(i,idx);
}
}
产品。添加(v);
}
退货产品;
}
映射值=新的HashMap();
value.put(1,Arrays.asList(“Charles”);
value.put(2,Arrays.asList(“Moulliard”);
values.put(3,array.asList(“Camel-in-Action”,“Camel-in-Action 2”);
value.put(4,Arrays.asList(“2009”、“2011”));
value.put(5,Arrays.asList(“ISBN”、“ISBN”、“ISBN”));
value.put(6,Arrays.asList(“1234”、“9876”、“7777”);
对于(列表l:s.产品(价值)){
系统输出打印LN(l);
}
}
公共列表产品(映射值){
列表产品=新的ArrayList();
映射索引=新的HashMap();
布尔incIndex=false;
而(!incIndex){
incIndex=真;
列表v=新的ArrayList();
for(int i=1;i++){
列表l=值。获取(i);
if(l==null){
打破
}
int-idx=0;
if(索引containsKey(i)){
idx=索引.get(i);
}
v、 添加(l.get(idx));
if(incIndex){
如果(++idx>=l.size()){
idx=0;
}否则{
incIndex=假;
}
ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
for (Author author:authors) {
    for (Book book:author.getBooks()) {
        for (Reference reference:book.getReferences()){
            for (Editor editor:editors) {
                table.add(new ArrayList<String>(Arrays.toList({author.getFirstName(), 
                                            author.getLastName(), 
                                            book.getTitle(), 
                                            book.getYear(), 
                                            reference.getType(), 
                                            reference.getCode(), 
                                            editor.getName()})
                                    );
                );
            }
        }
    }
}
        Map<Integer, List> values = new HashMap<Integer, List>();
    values.put(1, Arrays.asList("Charles"));
    values.put(2, Arrays.asList("Moulliard"));
    values.put(3, Arrays.asList("Camel in Action", "Camel in Action 2"));
    values.put(4, Arrays.asList("2009", "2011"));
    values.put(5, Arrays.asList("ISBN", "ISBN", "ISBN"));
    values.put(6, Arrays.asList("1234", "9876", "7777"));
    for (List l : s.product(values)) {
        System.out.println(l);
    }



}

public List<List> product(Map<Integer, List> values) {

       List<List> product = new ArrayList<List>();
       Map<Integer, Integer> index = new HashMap<Integer, Integer>();
       boolean incIndex = false;

       while (!incIndex) {

           incIndex = true;
           List v = new ArrayList();

           for (int i = 1; ; i++) {
               List l = values.get(i);
               if (l == null) {
                   break;
               }
               int idx = 0;
               if (index.containsKey(i)) {
                   idx = index.get(i);
               }
               v.add(l.get(idx));
               if (incIndex) {
                   if (++idx >= l.size()) {
                       idx = 0;
                   } else {
                       incIndex = false;
                   }
                   index.put(i, idx);
               }
           }
           product.add(v);
       }
       return product;
}