Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map of maps and generics

I want to create a (recursive) map of maps. That is, the value of type of the Map is another Map of the same type as the outer map.

For example:

Map<String, Map<String, Map<String, ... >>>> foo;

Evidently, I need some way to refer to "the type being defined" or something in order to do this. I guess I could do:

Map<String, Map<String, ?>>

... and then just @SupressWarnings("unchecked") myself past the inevitable warnings, but is there a better way?

like image 793
BeeOnRope Avatar asked Sep 11 '11 07:09

BeeOnRope


1 Answers

Create an auxiliary class or interface to refer to "the type being defined". Like this:

class MyMap extends HashMap<String, MyMap> {
    ...
}

or

interface MyMap extends Map<String, MyMap> {

}

(I don't think you can do without such auxiliary class / interface. When "recursing" you need a name to refer to.)

like image 170
aioobe Avatar answered Nov 04 '22 17:11

aioobe