I have seen this similar question in nested hashmaps of unknown depth java post but did not get an appropriate answer. I need the exact requirement, I need to have a nested hashmap with dynamic depth.
Please note that this problem is not implementation of Tree, it is something else.
We know that we can implement graph using hashmap. I can represent a graph with a hashmap as follows,
HashMap<Integer, HashMap<Integer, ArrayList<Integer>>>
The first Integer represents the vertex and the inside HashMap represents the neighborhood around that vertex. Now suppose that I want to implement the graph as follows:
I want The first Integer to represent the vertex and the inside HashMap to represent the neighborhood of neighborhood of neighborhood around that vertex. Then I need something like:
HashMap<Integer, HashMap<Integer, HashMap<Integer , HashMap<Integer ,ArrayList<Integer>>>>>
This is my question. The needed number of neighborhoods (hashmap depth) will be known in runtime (User decides that), Is there any way to implement this kind of functionality in Java?