Java data structure to simulate the data tree

I need help defining the method to use I have a soap response and give me an XML file I need to finally display 3 related lists on the screen When you select an item in the first list, the corresponding option will appear in the second list, etc I'm only interested in how to organize data effectively after extracting data from XML streams This is an XML fragment:

<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>

Therefore, I will have manufacturers = {Acer "," Acer "," Samsung "}, models = {A1", "A1", "A2", "e400"}, platforms = {robot "," J2ME "," robot "," robot "}

Interestingly, I need massage data so that I can use it to display 3 lists After selecting Android, Acer and Samsung can be listed If Acer is selected, models A1 and A2 can be used All lists need to be sorted At present, I am using Sax to parse data into object vectors, including manufacturer, model and platform fields All I can think of is a treemap like structure Any suggestions would be appreciated

Solution

I don't think we need a hierarchy here Because the user can choose the first platform or manufacturer If he chooses the first Android, three devices will be displayed If he chooses the first Acer, he will see two devices

Therefore, my suggestions are as follows

>Create class devices using attributes, manufacturer, model, and platform. > Create a pure link list containing all these devices. > Create two maps: manufacturerindex and platformindex, as follows: Map < string, collection < device > > manufacturerindex; > Iterate over the list once and populate all index mappings

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

Note that this data structure is extensible You can always add as many indexes as you need

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>