将java对象树转换为二维表

发布于 2024-08-08 01:20:51 字数 1541 浏览 7 评论 0原文

我面临以下问题。我有一个 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”, “代码”、“名称” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“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” “charles”,“moulliard”,“camel in action 2”,“2011”,“ISBB”,“1111”,“manning 2”,“43”

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

问候,

查尔斯

I'm faced to the following problem. I have a tree of java objects for which I have to export each field value into a CSV file. The result of the exportation must be similar to what we have in SQL left outer join (called cartesian product).

Class author

@DataField(pos = 1)
String firstName;

@DataField(pos = 2)
String lastName;

@OneToMany
List<Book> books;

@OneToMany
List<Editor> editors;

@DataField(pos = 7)
String Age;

Class Book

@DataField(pos = 3)
String title;

@DataField(pos = 4)
String year;

@OneToMany
List<Reference> references;

Class Reference

@DataField(pos = 5)
String type;

@DataField(pos = 6)
String code;

Class Editor

@DataField(pos = 8)
String name;

Remarks :
- @DataField annotation indicates the position of the value in the CSV record
- For this example we have one object Author (Charles, Moulliard) containing a list of 2 books ("Camel in action" and "Camel in action 2"). The first book has three Reference (ISBN 1234, ISBN 5678, ISBN 999) and the second one reference (ISBB 1111). Author contain also a list of two editors ("manning", "manning 2")

Here is an example and the result wanted

"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","camel in action","2009","ISBN","9999","manning","43"
"charles","moulliard","camel in action","2009","ISBN","9999","manning 2","43"
"charles","moulliard","camel in action 2","2011","ISBB","1111","manning","43"
"charles","moulliard","camel in action 2","2011","ISBB","1111","manning 2","43"

I have tried using a recursive function to put field values in Map of LinkedList : Map where Integer = position of the field in the CSV and Linkedlist = list of objects but I lost information about position of the element in the tree.

Regards,

Charles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-08-15 01:20:51

我可能不明白你的问题。这样的事情行不通吗?

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()})
                                    );
                );
            }
        }
    }
}

I probably don't understand your problem. Wouldn't something like this work?

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()})
                                    );
                );
            }
        }
    }
}
心舞飞扬 2024-08-15 01:20:51
        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;
}
        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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文