What is the best way in Java to keep values ("o") in a tree structure like this:
obj1
/\
/ \
/ \
obj2 obj3
/\ /\
/ \ / \
/ \ / \
obj4 obj5 obj6 obj7
/\ /\ /\ /\
/ \ / \ / \ / \
o8 oN...
It looks like a tree, but I don't need arbitrary depth. I rather need strong datatyping and predefined good looking methods for working with final structure.
I need to be able to get some kind of list of values by keys - exactly like on my picture. In other words, structure should not become planar in any way.
I need .get(obj3)
to return {obj6, obj7}, .get(obj1) - {obj2, obj3}
.
For now I use Map for that, but inflating such map is ugly, because I need to check each level of the structure. Looks like that (data is the map):
if(data.get(somedouble) == null) {
Map<Integer, Data> inm = new TreeMap<>();
inm.put(someint, obj);
Map<Double, Map<Integer, Data>> m = new TreeMap<>();
m.put(somedouble2, inm);
data.put(somedouble, m);
}
else {
if(data.get(somedouble).get(somedouble2) == null) {
Map<Integer, Data> inm = new TreeMap<>();
inm.put(someint, obj);
data.get(somedouble).put(somedouble2, inm);
}
else
data.get(somedouble).get(somedouble2).put(someint, obj);
}
Performance in not an issue, but code beauty is.