java数据结构来模拟数据树 [英] java data-structure to simulate a data tree

查看:189
本文介绍了java数据结构来模拟数据树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助定义使用哪种方法。我有一个SOAP响应,给我一个xml文件。我需要最终在屏幕上显示3个相关列表。当您在第一个列表中选择一个项目时,相应的选项将显示在第二个列表中。我只对在从xml流中提取数据后如何有效地组织数据感兴趣。这是一个xml片段:

 < device> 
<制造商>宏碁< /制造商>
< model> A1< / model>
< platform> Android< / platform>
< / device>
< device>
<制造商>宏碁< /制造商>
< model> A1< / model>
< platform> J2ME< / platform>
< / device>
< device>
<制造商>宏碁< /制造商>
< model> A2< / model>
< platform> Android< / platform>
< / device>
< device>
<制造商>三星< / manufacturer>
< model> E400< / model>
< platform> Android< / platform>
< / device>因此,我将会有一些像制造商= {宏碁,宏碁,宏碁)这样的东西, ,Samsung},model = {A1,A1,A2,E400},platform = {Android,J2ME,Android,Android}



这里有趣的部分:我需要按摩数据,以便我可以使用它来显示3个列表。选择Android后,宏碁和三星就可以使用。如果选择宏碁,则A1和A2型号可用。所有列表都需要排序。目前我正在使用Sax将数据解析成一个对象的向量,包含制造商,模型,平台字段。我可以想到的是一个TreeMap像结构。任何建议,将不胜感激。

解决方案

我不认为层次结构是你在这里需要的。因为用户可以选择第一平台或制造商。如果他选择第一个Android,你想显示3个设备。如果他选择了第一台宏碁,他会看到2台设备。



所以,我的建议如下。


  1. 创建类别具有属性制造商,型号,平台的设备。

  2. 创建包含所有这些设备的简单链接列表。

  3. 创建2个地图:manufaturerIndex和plarformIndex,如下所示:

    Map< String,Collection< Device>>制造商指数;


  4. 在列表中迭代一次,并填充所有索引地图。


像这样:

  for(Device d:devices){
Collection< Device> selected = manufacturerIndex.get(d.getManufacturer());
if(selected == null){
selected = new ArrayList< Device>();
manufactuerIndex.put(d.getManufacturer(),selected);
}
selected.add(d);
//第二个索引相同
}

现在可以使用数据结构



manufactuerIndex.get(Nokia) - >返回所有诺基亚设备。 >

注意这个数据结构是可扩展的。您可以随时添加任意数量的索引。


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.

解决方案

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.

  1. create class Device with properties manufacturer, model, platform.
  2. create a plain linked list that contains all these devices.
  3. Create 2 maps: manufaturerIndex and plarformIndex that look like:
    Map<String, Collection<Device>> manufacturerIndex;

  4. Iterate once over the list and populate all indexes maps.

Like this:

for(Device d : devices) {
    Collection<Device> selected = manufacturerIndex.get(d.getManufacturer());
    if (selected == null) {
         selected = new ArrayList<Device>();
         manufactuerIndex.put(d.getManufacturer(), selected);
    }
    selected.add(d);
    // the same for the second index
}

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.

这篇关于java数据结构来模拟数据树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆