user479576
user479576

Reputation: 35

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 ?

Upvotes: 0

Views: 156

Answers (2)

Adriaan Koster
Adriaan Koster

Reputation: 16209

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.

Upvotes: 0

Thomas
Thomas

Reputation: 88707

If you want to map "text1" to "text2" and this to a list of objects, why not just use a nested map: Map<String, Map<String, List<Obj>>>?

Upvotes: 2

Related Questions