简体   繁体   中英

What is the best Object based data structure for this hierarchy?

What is the best Object based data structure for this hierarchy

"text1"
   |---> "text2"
            |-----> List of objs

I could do something like

class Test(){
      String text1;
      HashMap<String,List<Obj>> text2ListOfObjs;
}

The consumer of this class would do something like

for(Test test: tests){
   iterate over hashmap
      ....
}

Any other suggestions ?

如果要将“text1”映射到“text2”并将其映射到对象列表,为什么不使用嵌套映射: Map<String, Map<String, List<Obj>>>

Object oriented designs should convey meaning. You should give us the actual context of this hierarchy instead of the abstract elements. Any data structure could be used to reflect the elements you present, but it would not necessarily be a good design, depending on the meaning, behavior and context.

A few possible structures:

class Person {
    String name;
    Relation relation;
}

class Relation {
    String type;
    List<Person> members;
}

For example a Person with name 'Arnie' and a Relation with type "friends" and as members the Persons with names "Bert", "Minnie" and "Joel".

class Song {
    String title;
    String composer;
    List<Note> notes;
}

Or an even flatter model:

Object[] data;

which we could instantiate with:

data = new Object[] { "text1", "text2", new Object(), new Integer(), new BlaBla() };

All these are 'object oriented' models fitting the bill, however which is the best depends on what you are trying to represent and how the system should behave.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM