简体   繁体   English

地图 <String, List<? extends AbstractType> &gt;

[英]Map<String, List<? extends AbstractType>>

I was looking all around Stack Overflow but I don't know if that's possible but here is my question: 我一直在四处寻找Stack Overflow,但我不知道是否可行,但这是我的问题:

I have two types A and B extending from AbstractType , then I want a Map with both type of object inside like this: 我有两个从AbstractType扩展的类型AB ,然后我想要一个Map ,里面有两种对象:

Map<String,List<? extends AbstractType>> map = new HashMap();
map.put("toto",new Arraylist<A>);
map.put("sacha",new Arraylist<B>);
List<A> a = (List<A>)map.get("toto");

I have a safety warning which is completely understandable, but that's the best way I found to make this work. 我有一个完全可以理解的安全警告,但这是我发现进行此工作的最佳方法。

The only safety option I found was to have two distinct lists, but I would rather prefer a map to avoid multiple if case. 我发现的唯一安全选择是有两个不同的列表,但是我宁愿选择一个映射来避免多个if情况。

I hope someone will have an idea. 我希望有人能有个主意。

You can also use instanceof to always make sure that you don't get in some trouble. 您还可以使用instanceof始终确保不会遇到麻烦。 Here is an example, you can modify it as per your requirements: 这是一个示例,您可以根据需要对其进行修改:

Map<String,List<? extends AbstractType>> map = new HashMap<>();
        map.put("toto",new ArrayList<A>());
        map.put("sacha",new ArrayList<B>());

        List<? extends AbstractType> objList = (List<? extends AbstractType>) map.get("toto");
        List<A> a = null;
        List<B> b = null; 
        if(objList.size() > 0 && objList.get(0) instanceof A){
            a = (List<A>) objList;
        }else if (objList.size() > 0 && objList.get(0) instanceof B){
            b = (List<B>) objList;
        }

Hope this helps! 希望这可以帮助!

暂无
暂无

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

相关问题 List <Map <String,String >> vs List <?扩展Map <String,String >> - List<Map<String, String>> vs List<? extends Map<String, String>> 如何使用List <? extends Map<String, ?> &gt; - How to use List<? extends Map<String, ?>> 如何更新列表中的数据 <? extends Map<String,?> &gt; - How to update data in a List<? extends Map<String,?>> Java泛型放在地图上 <String, ? extends List<String> &gt; - Java Generics putting on Map<String, ? extends List<String>> Map <string, list<? extends object> &gt; Stream API 的过滤问题</string,> - Map<String, List<? extends Object>> filtering issue with Stream API 带通配符的泛型(地图 <? extends A, List<? extends B> &gt;) - Generics with Wildcards (Map<? extends A, List<? extends B>>) 自定义视图布局-“列表 <Map> 无法转换为列表 <? extends Map<String,?> &gt;” - Custom View Layout - “ List<Map> cannot be converted to List<? extends Map<String,?>> ” Android-通过多个列表 <AbstractType> 到BaseAdapter - Android - Pass Multiple List<AbstractType> to BaseAdapter 我可以定义地图吗 <String, List<? extends BaseView> &gt;这样我就可以从地图中获取()列表而无需进行未经检查的转换? - Can I define a Map<String, List<? extends BaseView>> so that I can get() a list from the map without unchecked casting? 我应该为List类型的参数输入什么值 <? extends Map<String, ?> &gt;? - What values should I put for a parameter with type the of List<? extends Map<String, ?>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM