从csv平面文件填充JavaBean树结构的算法
我目前正试图从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可以有无限多的属性和无限多的子组,每个子组也有子组
Being说,我试图阅读这个简单的csv描述,将其存储在这个bean结构中:
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文件是可以的,我一直在研究如何使用递归存储描述数据,但找不到方法
提前感谢您的任何算法想法
我希望这会让你有心情考虑这个。。。我得承认我没有主意了
# 1 楼答案
这里有一个解决方案,添加评论只是一个练习
# 2 楼答案
# 3 楼答案
您可以尝试以更一般的方式查看问题,请查看以下内容:
我的
Node
类有一个对象(Data
)的Getter
和Setter
,该对象在其他属性中有一个GroupId
和一个ParentId
属性