java数据结构模拟数据树
我需要帮助定义使用什么方法。我有一个 SOAP 响应,给我一个 xml 文件。我需要在屏幕上显示 3 个相关列表。当您在第一个列表中选择一个项目时,相应的选择将出现在第二个列表中,依此类推。我只对从 xml 流中提取数据后如何有效地组织数据感兴趣。这是一个 xml 片段:
<device>
<manufacturer>Acer</manufacturer>
<model>A1</model>
<platform>Android</platform>
</device>
<device>
<manufacturer>Acer</manufacturer>
<model>A1</model>
<platform>J2ME</platform>
</device>
<device>
<manufacturer>Acer</manufacturer>
<model>A2</model>
<platform>Android</platform>
</device>
<device>
<manufacturer>Samsung</manufacturer>
<model>E400</model>
<platform>Android</platform>
</device>
所以,我会有类似制造商={"Acer", "Acer", "Acer","Samsung"}, model={"A1","A1", "A2", "E400" },平台={“Android”,“J2ME”,“Android”,“Android”}。
有趣的部分来了:我需要处理数据,以便我可以用它来显示 3 个列表。选择Android后,Acer和Samsung就可用了。如果选择Acer,则有A1和A2型号可供选择。所有列表都需要排序。目前,我正在使用 Sax 将数据解析为对象向量,其中包含制造商、型号、平台字段。我能想到的只是一个类似于 TreeMap 的结构。任何建议将不胜感激。
I need help defining what approach to use. I have a SOAP response giving me an xml file. I need to end-up with 3 correlated lists displayed on screen. When you select one item on the first list, the corresponding choices will appear on the second list etc. I am only interested on how to organize efficiently the data after it is extracted from the xml stream. Here's an xml snippet:
<device>
<manufacturer>Acer</manufacturer>
<model>A1</model>
<platform>Android</platform>
</device>
<device>
<manufacturer>Acer</manufacturer>
<model>A1</model>
<platform>J2ME</platform>
</device>
<device>
<manufacturer>Acer</manufacturer>
<model>A2</model>
<platform>Android</platform>
</device>
<device>
<manufacturer>Samsung</manufacturer>
<model>E400</model>
<platform>Android</platform>
</device>
So, I will have something like manufacturer={"Acer", "Acer", "Acer","Samsung"}, model={"A1","A1", "A2", "E400"}, platform={"Android","J2ME","Android","Android"}.
Here comes the fun part: I need to massage the data so that I can use it to display 3 lists. After selecting Android, Acer and Samsung become available. If Acer is selected, then model A1 and A2 are available. All lists need to be sorted. Currently I'm using Sax to parse the data into a vector of objects, containing manufacturer, model, platform fields. All I can think of is a TreeMap like structure. Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为层次结构不是您所需要的。因为用户可能会选择第一个平台或制造商。如果他选择第一个 Android,您要显示 3 台设备。如果他选择第一个 Acer,他将看到 2 台设备。
所以,我的建议如下。
创建 2 个地图:manufaturerIndex 和 plarformIndex,如下所示:
地图<字符串,集合<设备>> ManufacturerIndex;
遍历列表一次并填充所有索引映射。
像这样:
现在您可以使用数据结构了。
manufactuerIndex.get("诺基亚")
->返回所有诺基亚设备。注意这个数据结构是可扩展的。您始终可以根据需要添加任意数量的索引。
I do not think that hierarchical structure is what you need here. Because user may select first platform or manufacturer. If he selects first Android you want to show 3 devices. If he selects first Acer he will see 2 devices.
So, my suggesting is the following.
Create 2 maps: manufaturerIndex and plarformIndex that look like:
Map<String, Collection<Device>> manufacturerIndex;
Iterate once over the list and populate all indexes maps.
Like this:
Now you can use the data structure.
manufactuerIndex.get("Nokia")
-> returns all Nokia devices.Pay attention that this data structure is extendable. You can always add as many indexes as you want.
我只需使用自定义对象的可排序集合,然后根据谓词过滤该集合。我使用 Guava 来完成所有这些,但当然还有其他(通常更多)复杂)的方法来实现这一点。
这是我的产品对象:
现在我只需使用
TreeSet
并在其上应用视图。下面是一个返回按模型过滤的实时视图的示例方法:像这样使用它:
更新: 我们可以更进一步,仅使用一个集合,由依次绑定的链式谓词支持到一个由你的观点支持的模型。脑子疼吗?检查一下:
yourViewModel
是一个由表单控制器返回的值支持的对象。每个谓词使用该模型对象的一个字段来决定它是否适用。例如,
ModelPredicate
检查集合中的所有产品,以查看其模型是否在所选产品之中。由于这使用了and
逻辑,因此您可以将其设为分层结构(如果制造商谓词返回 false,则永远不会调用模型和平台谓词)。I'd just use a sortable collection of custom objects and then filter that collection based on predicates. I am using Guava for all of this, but there are of course other (usually more complicated) ways to implement this.
Here's my Product Object:
Now I'd just use a
TreeSet<Product>
and apply views on it. Here's a sample method that returns a live view that is filtered by model:Use it like this:
Update: We can take it even further, using only one collection, backed by chained predicates that are in turn tied to a model backed by your view. Brain hurts? Check this out:
yourViewModel
is an object that is backed by the values returned from your form controller. Each predicate uses a field of this model object to decide whether it applies or not.e.g. The
ModelPredicate
checks all products in the collection to see whether their model is among the selected ones. Since this usesand
logic, you can make it a hierarchic structure (if the manufacturer predicate returns false, the model and platform predicates are never called).我使用嵌套地图来实现类似的目的。使用
TreeMap
获取排序结果:I use nested maps for something like that. Use
TreeMap
to get sorted results: