从 csv 平面文件填充 Java Bean 树结构
我目前正在尝试从 csv 格式的平面描述文件构建 Java 中的 bean 类列表。具体来说:
这是csv文件的结构:
MES_ID;GRP_PARENT_ID;GRP_ID;ATTR_ID
M1 ; ;G1 ;A1
M1 ; ;G1 ;A2
M1 ;G1 ;G2 ;A3
M1 ;G1 ;G2 ;A4
M1 ;G2 ;G3 ;A5
M1 ; ;G4 ;A6
M1 ; ;G4 ;A7
M1 ; ;G4 ;A8
M2 ; ;G1 ;A1
M2 ; ;G1 ;A2
M2 ; ;G2 ;A3
M2 ; ;G2 ;A4
它对应于分层数据结构:
M1
---G1
------A1
------A2
------G2
---------A3
---------A4
---------G3
------------A5
---G4
------A7
------A8
M2
---G1
------A1
------A2
---G2
------A3
------A4
备注:
一条消息M可以有无限多个组G和属性A
组 G 可以具有无限数量的属性和无限数量的下组,每个下组也具有下组
蜜蜂说,我正在尝试读取这个平面 csv 描述以将其存储在这种 beans 结构中:
Map<String, MBean> messages = new HashMap<String, Mbean>();
==
public class MBean {
private String mes_id;
private Map<String, GBean> groups;
}
public class GBean {
private String grp_id;
private Map<String, ABean> attributes;
private Map<String, GBean> underGroups;
}
public class ABean {
private String attr_id;
}
按顺序读取 csv 文件是可以的,我'一直在研究如何使用递归来存储描述数据,但找不到方法。
预先感谢您的任何算法想法。
我希望它能让你思考这个问题......我不得不承认我没有想法:s
I'm currently trying to construct a list of bean classes in Java from a flat description file formatted in csv. Concretely :
Here is the structure of the csv file :
MES_ID;GRP_PARENT_ID;GRP_ID;ATTR_ID
M1 ; ;G1 ;A1
M1 ; ;G1 ;A2
M1 ;G1 ;G2 ;A3
M1 ;G1 ;G2 ;A4
M1 ;G2 ;G3 ;A5
M1 ; ;G4 ;A6
M1 ; ;G4 ;A7
M1 ; ;G4 ;A8
M2 ; ;G1 ;A1
M2 ; ;G1 ;A2
M2 ; ;G2 ;A3
M2 ; ;G2 ;A4
It corresponds to the hierarchical data structure :
M1
---G1
------A1
------A2
------G2
---------A3
---------A4
---------G3
------------A5
---G4
------A7
------A8
M2
---G1
------A1
------A2
---G2
------A3
------A4
Remarks :
A message M can have an infinite number of groups G and attributes A
A group G can have an infinite number of attributes and an infinite number of under-groups each of them having under-groups too
That beeing said, I'm trying to read this flat csv decription to store it in this structure of beans :
Map<String, MBean> messages = new HashMap<String, Mbean>();
==
public class MBean {
private String mes_id;
private Map<String, GBean> groups;
}
public class GBean {
private String grp_id;
private Map<String, ABean> attributes;
private Map<String, GBean> underGroups;
}
public class ABean {
private String attr_id;
}
Reading the csv file sequentially is ok and I've been investigating how to use recursion to store the description data, but couldn't find a way.
Thanks in advance for any of your algorithmic ideas.
I hope it will put you in the mood of thinking about this ... I've to admit that I'm out of ideas :s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个解决方案,添加评论留作练习......
Here is a solution, adding comments is left as an exercise...
您可以尝试以更一般的方式查看问题,请看下面:
我的 Node 类有一个 Getter 和 Setter 用于对象 (
Data
),其中包含GroupId
和ParentId
属性You could try to view the problem in a bit more general way, have a look below:
My
Node
class has aGetter
andSetter
for an object (Data
) which among other properties has aGroupId
and aParentId
property