将java对象树转换为2维表

时间:2009-10-15 10:42:30

标签: 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)
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记录中值的位置 - 在这个例子中,我们有一个对象作者(Charles,Moulliard),其中包含2本书的清单(“Camel in action”和“Camel in action 2”)。第一本书有三个参考书(ISBN 1234,ISBN 5678,ISBN 999)和第二本参考书(ISBB 1111)。作者还包含两个编辑的列表(“manning”,“manning 2”)

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

“名字”, “姓氏”, “年龄”, “标题”, “年”, “类型”, “代码”, “名” “charles”,“moulliard”,“camel in action”,“2009”,“ISBN”,“1234”,“manning”,“43” “charles”,“moulliard”,“骆驼在行动”,“2009”,“ISBN”,“1234”,“manning 2”,“43” “charles”,“moulliard”,“骆驼在行动”,“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”,“骆驼在行动”,“2009”,“ISBN”,“9999”,“manning 2”,“43” “查尔斯”,“moulliard”,“骆驼在行动2”,“2011”,“ISBB”,“1111”,“曼宁”,“43” “查尔斯”,“moulliard”,“骆驼在行动2”,“2011”,“ISBB”,“1111”,“曼宁2”,“43”

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

此致

查尔斯

2 个答案:

答案 0 :(得分: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()})
                                    );
                );
            }
        }
    }
}

答案 1 :(得分: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;
}
相关问题