gpt4 book ai didi

java - 从 csv 平面文件填充 Java Bean 树结构

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:35:44 26 4
gpt4 key购买 nike

我目前正在尝试从一个 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 描述以将其存储在这种 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文件是可以的,我一直在研究如何使用递归来存储描述数据,但找不到方法。

提前感谢您提供任何算法想法。

我希望它会让你有心情去思考这个……我不得不承认我没有想法:s

最佳答案

这是一个解决方案,添加评论留作练习......

public class Test {

public static void main(String[] args) {
List<Record> records = new ArrayList<Record>() {
{
add(new Record("M1", "", "G1", "A1"));
add(new Record("M1", "", "G1", "A2"));

add(new Record("M1", "G1", "G2", "A3"));
add(new Record("M1", "G1", "G2", "A4"));
}
};

MessageContainer messageContainer = new MessageContainer();
for (Record record : records) {
messageContainer.addOrUpdateMessage(record);
}

}

private static class Record {

final String messageId;
final String parentGroupId;
final String groupId;
final String attributeId;

public Record(String messageId, String parentGroupId, String groupId, String attributeId) {
super();
this.messageId = messageId;
this.parentGroupId = parentGroupId;
this.groupId = groupId;
this.attributeId = attributeId;
}

}

private static class MessageContainer {

Map<String, MBean> messages = new HashMap<String, MBean>();

public void addOrUpdateMessage(Record record) {
MBean mBean = messages.get(record.messageId);
if (mBean == null) {
mBean = new MBean(record.messageId);
messages.put(record.messageId, mBean);
}
mBean.addOrUpdateGroup(record);
}
}

private static class MBean {
private final String mes_id;
private final Map<String, GBean> groups = new HashMap<String, GBean>();

public MBean(String mesId) {
super();
mes_id = mesId;
}

public void addOrUpdateGroup(Record record) {
String groupToHandle = (record.parentGroupId != "" ? record.parentGroupId : record.groupId);
GBean gBean = groups.get(groupToHandle);
if (gBean == null) {
gBean = new GBean(groupToHandle);
groups.put(groupToHandle, gBean);
}
gBean.addOrUpdateGroup(record);
}
}

private static class GBean {
private final String groupId;
private final Map<String, ABean> attributes = new HashMap<String, ABean>();
private final Map<String, GBean> underGroups = new HashMap<String, GBean>();

public GBean(String groupId) {
super();
this.groupId = groupId;
}

public void addOrUpdateGroup(Record record) {
if (groupId == record.parentGroupId) {

GBean child = underGroups.get(record.groupId);
if (child == null) {
child = new GBean(record.groupId);
underGroups.put(record.groupId, child);
}
child.addOrUpdateGroup(record);

} else if (groupId == record.groupId) {
attributes.put(record.attributeId, new ABean(record.attributeId));
} else {
throw new RuntimeException("Unexpected state [recordParentGroupId="+record.parentGroupId+", recordGroupId="+record.groupId+", groupId="+groupId+"]");

}

}

private static class ABean {
private final String attr_id;
public ABean(String attrId) {
super();
attr_id = attrId;
}

}
}
}

关于java - 从 csv 平面文件填充 Java Bean 树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2459258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com