简体   繁体   English

您可以在地图值上指定泛型类型吗?

[英]Can you specify generics type on map value?

In Java (5+) you can do: 在Java(5+)中,您可以执行以下操作:

TreeMap<String, List> map = someObject.getMap();

But can specify the generic type within the List ? 但是可以在List指定泛型吗? A la: 啦啦

TreeMap<String, List<String>> map = someObject.getMap();

?? ??

You sure can. 你当然可以。 Works great. 效果很好。

Yes you can. 是的你可以。 Do you have a compiler error in your code you want help fixing? 您想要帮助修复的代码中是否存在编译器错误?

Yes,why not? 是的,为什么不? But keep in mind that the type gets erased in the compiled code, so isn't runtime available. 但是请记住,该类型会在编译后的代码中删除,因此运行时不可用。 That can be a problem if you access it with reflection. 如果以反射方式访问它,可能会成为问题。

You can nest generic types to arbitrary number of levels. 您可以将泛型类型嵌套到任意数量的级别。 Eg : 例如:

 TreeMap<String, List<Map<String, String>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM