简体   繁体   English

此层次结构的最佳基于对象的数据结构是什么?

[英]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". 例如,名为“Arnie”的人员和类型为“friends”的人员以及名为“Bert”,“Minnie”和“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. 所有这些都是适合该法案的“面向对象”模型,但这最好取决于您要表示的内容以及系统应如何表现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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