Add subclasses to ConcurrentHashMap of super type in a static initializer?

此生再无相见时 提交于 2019-12-13 18:29:34

问题


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>>)


回答1:


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);
    }

}



回答2:


Because a Collection<family> is not a Collection<people>. Put another way: Java collections are not covariant.

Is there a way I could put family into the hashmap?

Declare it as a Collection<people>.




回答3:


family is convertible to people, but Collection<family> is not convertible to Collection<people>.
Had it been convertible, you would have been able to unsafely add a different derived tyupe to the casted collection.

Instead, you can use a covariant view of the collection type:

ConcurrentMap<String, Collection<? extends people>>


来源:https://stackoverflow.com/questions/17200675/add-subclasses-to-concurrenthashmap-of-super-type-in-a-static-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!