在静态初始化程序中将子类添加到超类型的ConcurrentHashMap中? [英] Add subclasses to ConcurrentHashMap of super type in a static initializer?

查看:143
本文介绍了在静态初始化程序中将子类添加到超类型的ConcurrentHashMap中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class People {

class Family extends People {

}

}


public class Together {
private static Collection< Family> familyList = new ArrayList< Family>();
private static ConcurrentMap< String,Collection< People>> registry = new ConcurrentHashMap< String,Collection< People>>();

static {
registry.put(Family.class.toString(),familyList);


$ / code $ / pre

错误信息:

 方法put(String,Collection< people>)类型为Map< String,Collection< people>>不适用于参数(String,Collection< family>)

为什么我不能把 familyList 注册表?我认为,因为 family 扩展了 people ,所以我应该能够将子类型放入超类型 registry



编辑:上面已解决。这个问题的最后一部分涉及到一个更复杂的例子:

  public class Together {
private static ConcurrentMap< String,Collection< Family>> familyMap = new ConcurrentHashMap< String,Collection< Family>>();
private static ConcurrentMap< String,ConcurrentMap< String,Collection< People>>> registry2 = new ConcurrentHashMap< String,ConcurrentMap< String,Collection< People>>>();

static {
registry2.put(Family.class.toString(),familyMap);


$ / code $ / pre

(我已经试过改变 registry2 具有?extends People



现在错误是:
类型Map< String,ConcurrentMap< String,Collection< People>>中的方法put(String,ConcurrentMap< String,Collection< People>>)不适用于(String,ConcurrentMap< String,Collection< Family>>) 试试这个:



people.java

  public class people {

public class family extends people {

}

public static void main(String [] args){
together t = new together();
System.out.println(together.registry);
}

}

together.java

  import java.util.ArrayList; 
import java.util.C ollection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

公共课程{b $ b private static Collection< people.family> familyList = new ArrayList< people.family>();
public static ConcurrentMap< String,Collection< ;?扩展人员>> registry = new ConcurrentHashMap< String,Collection <?扩展人员>>();

static {
registry.put(people.family.class.toString(),familyList);
}

}


public class People {

    class Family extends People {

    }

}


public class Together {
    private static Collection<Family> familyList = new ArrayList<Family>();
    private static ConcurrentMap<String, Collection<People>> registry = new ConcurrentHashMap<String, Collection<People>>();

    static {
        registry.put(Family.class.toString(), familyList); 
    }
}

Error message:

The method put(String, Collection<people>) in the type Map<String,Collection<people>> is not applicable for the arguments (String, Collection<family>)

Why can't I put familyList into registry? I figure that since family extendspeople that i should be able to place the sub types into the super type registry .

EDIT: The above is solved. My last part of the question involves a more complicated example using the same names:

public class Together {
    private static ConcurrentMap<String, Collection<Family>> familyMap= new ConcurrentHashMap<String, Collection<Family>>();
    private static ConcurrentMap<String, ConcurrentMap<String, Collection<People>>> registry2 = new ConcurrentHashMap<String, ConcurrentMap<String, Collection<People>>>();

    static {
        registry2.put(Family.class.toString(), familyMap); 
    }
}

(I already tried changing the declaration of registry2 to having ?extends People

Now the error is: The method put(String, ConcurrentMap<String,Collection<People>>) in the type Map<String,ConcurrentMap<String,Collection<People>>> is not applicable for the arguments (String, ConcurrentMap<String,Collection<Family>>)

解决方案

Try this:

people.java

public class people {

    public class family extends people {

    }

    public static void main(String[] args) {
        together t = new together();
        System.out.println(together.registry);
    }

}

together.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class together {
    private static Collection<people.family> familyList = new ArrayList<people.family>();
    public static ConcurrentMap<String, Collection<? extends people>> registry = new ConcurrentHashMap<String, Collection<? extends people>>();

    static {
        registry.put(people.family.class.toString(), familyList);
    }

}

这篇关于在静态初始化程序中将子类添加到超类型的ConcurrentHashMap中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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