1

我面临以下问题。我有一个 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 = 3)
String title;

@DataField(pos = 4)
String year;

@OneToMany
List<Reference> references;

类参考

@DataField(pos = 5)
String type;

@DataField(pos = 6)
String code;

类编辑器

@DataField(pos = 8)
String name;

备注: - @DataField 注释指示值在 CSV 记录中的位置 - 对于此示例,我们有一个对象 Author (Charles, Moulliard),其中包含 2 本书的列表(“Camel in action”和“Camel in action 2”) . 第一本书有三个参考(ISBN 1234、ISBN 5678、ISBN 999)和第二个参考(ISBB 1111)。作者还包含两个编辑的列表(“manning”、“manning 2”)

这是一个例子和想要的结果

"firstName","lastName","age","title","year","type","code","name" "charles","moulliard","camel in action","2009"," ISBN","1234","manning","43" "charles","moulliard","camel in action","2009","ISBN","1234","manning 2","43" "charles ","moulliard","camel in action","2009","ISBN","5678","manning","43" "charles","moulliard","camel in action","2009"," ISBN","5678","manning 2","43""charles","moulliard","骆驼在行动","2009","ISBN","9999","manning","43","charles","moulliard","camel in action","2009","ISBN","9999","manning 2","43"" charles","moulliard","骆驼在行动 2","2011","ISBB","1111","manning","43" "charles","moulliard","骆驼在行动 2","2011 ","ISBB","1111","曼宁 2","43""ISBB","1111","manning","43" "charles","moulliard","骆驼在行动 2","2011","ISBB","1111","manning 2","43""ISBB","1111","manning","43" "charles","moulliard","骆驼在行动 2","2011","ISBB","1111","manning 2","43"

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

问候,

查尔斯

4

2 回答 2

1

我可能不明白你的问题。像这样的东西不会起作用吗?

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()})
                                    );
                );
            }
        }
    }
}
于 2009-10-15T11:25:45.250 回答
0
        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;
}
于 2009-10-15T11:59:01.650 回答