在哈希映射子类仿制药? [英] Subclass generics in hash maps?

查看:95
本文介绍了在哈希映射子类仿制药?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类人{}类朋友扩展人{}一流的同事扩展人{}类系列扩展人{}公共无效fillMaps(){
    ConcurrentMap<弦乐,收集和LT;家庭>> familyNames =新的ConcurrentHashMap<弦乐,收集和LT;家庭>>();
    ConcurrentMap<弦乐,收集和LT;同事>> coworkersNames =新的ConcurrentHashMap<弦乐,收集和LT;同事>>();
    ConcurrentMap<弦乐,收集和LT;朋友>> friendNames =新的ConcurrentHashMap<弦乐,收集和LT;朋友>>();
    populateMap(family.class,familyNames);
    populateMap(coworkers.class,coworkersNames);
    populateMap(friend.class,friendNames);
}私人< T>无效populateMap(类< T> clazz所ConcurrentMap<弦乐,收集和LT;人>>地图){
        如果(clazz所== family.class){
            map.put(榜样,新的ArrayList<家庭>());
        }
        如果(clazz所== coworkers.class){
            map.put(榜样,新的ArrayList<&同事GT;());
        }
        如果(clazz所== friend.class){
            map.put(榜样,新的ArrayList<&朋友GT;());
        }}

家人,同事,朋友和所有类延伸,同时超类的人。为什么不低于该方法允许我使用这个类作为一个参数populateMap方法的参数。另外,为什么没有让我传递一个子类集合作为这里的参数?

 错误:
该方法populateMap(类< T&GT ;, ConcurrentMap<弦乐,收集和LT;人>>)的不适用的参数(类<家庭>中ConcurrentMap<弦乐,收集和LT;家庭>>)


解决方案

的ArrayList<家庭> 不被视为收藏℃的亚型;人>
的ArrayList<家庭> 收藏&LT亚型;家庭>
的ArrayList<&人GT; 收藏&LT亚型;人>

您想拥有这个

 私人<吨扩展人>无效populateMap(ConcurrentMap<弦乐,收集和LT; T>>地图){                map.put(榜样,新的ArrayList< T>());
    }

 public class people {

}

class friend extends people {

}

class coworkers extends people {

}

class family extends people {

}

public void fillMaps(){
    ConcurrentMap<String, Collection<family>> familyNames = new ConcurrentHashMap<String, Collection<family>>();
    ConcurrentMap<String, Collection<coworkers>> coworkersNames = new ConcurrentHashMap<String, Collection<coworkers>>();
    ConcurrentMap<String, Collection<friend>> friendNames = new ConcurrentHashMap<String, Collection<friend>>();
    populateMap(family.class, familyNames);
    populateMap(coworkers.class, coworkersNames);
    populateMap(friend.class, friendNames);
}

private <T> void populateMap(Class<T> clazz, ConcurrentMap<String, Collection<people>> map) {
        if (clazz == family.class) {
            map.put("example", new ArrayList<family>());
        }
        if (clazz == coworkers.class) {
            map.put("example", new ArrayList<coworkers>());
        }
        if (clazz == friend.class) {
            map.put("example", new ArrayList<friend>());
        }

}

family, coworkers, and friend classes all extend from the superclass called people. Why doesn't the method below allow me to use the class as an argument to the parameters of the populateMap method. Also, why doesn't it allow me to pass a subclass collection as an argument here?

error:
The method populateMap(Class<T>, ConcurrentMap<String,Collection<people>>) is not applicable for the arguments (Class<family>, ConcurrentMap<String,Collection<family>>)

解决方案

ArrayList<family> isn't considered as a subtype of Collection<people> ArrayList<family> subtype of Collection<family> ArrayList<people> subtype of Collection<people>

you want to have this

private <T extends people> void populateMap(ConcurrentMap<String, Collection<T>> map) {

                map.put("example", new ArrayList<T>());


    }

这篇关于在哈希映射子类仿制药?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆