0

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?

hichkas
  • 43
  • 1
  • 6
  • 1
    Why wouldn't you just create an object which has a Hashmap attribute which you can then code around? This would allow you to recursively create sub-objects which contain the hashmap you need to work with...? – Gerik Mar 15 '18 at 03:39
  • Sorry, but I think the problem still remains unsolved. With your solution, lets suppose HashMap> as an object. How can I create a object of object of object of ... of object of object (the number of these repeats is decided on **runtime**)? My problem is how to implement this? – hichkas Mar 15 '18 at 03:51
  • No, I think you've misunderstood. Create a class MyClass which extends HashMap. MyClass has an attribute of MyClassAttribute which is a type MyClass (which is a HashMap). This allows you to create and access the code of the HashMap recursively in your code. If you also need the ArrayList it can be a second attribute. – Gerik Mar 15 '18 at 03:58
  • 1
    It's almost always a bad idea to extend collection types, but you can and should have, like, a `class Vertex { private final Map map; }` And to be clear, @hichkas, you _can't_ have types decided dynamically at runtime like that, but frankly wanting to usually implies a problem in your design. – Louis Wasserman Mar 15 '18 at 04:19
  • There's no typesafe way to statically represent a dynamic structure. You can use a `Map`, where each value can be either a `List` or another `Map`. You loose type safety, but you get what you want (but would now need to cast). As others say, you should consider creating a `Node` class that has both a list and a map as attributes. – fps Mar 15 '18 at 14:43
  • Thank you everyone, but I still can not implement that. Can someone please help with the implementation? Suppose I have a Obj class with the mentioned attributes and a create function: ** public class Obj { public ArrayList array; public HashMap hash; public create () {// what should I have here????}}** – hichkas Apr 04 '18 at 18:56

0 Answers0