Java – maps and generic maps

I want to create a (recursive) map That is, the type value of the map is another map of the same type as the external map

For example:

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

Obviously, I need some way to reference the "type being defined" or something else to do this I think I can:

Map<String,?>>

... then just @ suppresswarnings ("unchecked") surpasses the inevitable warnings, but is there a better way?

Solution

Create a helper class or interface to reference 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 classes / interfaces. When "recursive", you need a name to refer to.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>