Cast ConcurrentHashMap <String, B> to ConcurrentHashMap <String, A> when B implements A

I have an interface called SerializableL, which is implemented by three different classes:

  • Product
  • Banner
  • Tag

I started refactoring and wanted to replace several paragraphs with a few method calls.

public void load(ConcurrentHashMap<String, SerializableL> map, 
ArrayList<SerializableForL> preparedList)

I wrote the following code and got the following error.

The code:

ConcurrentHashMap<String, SerializableForL> test = DBStore.Cache.get(tag);

Error:

Type mismatch: cannot convert from ConcurrentHashMap<String,Banner> to 
    ConcurrentHashMap<String,SerializableForL>

How to make a workaround?

I need a way to translate ( ConcurrentHashMap<String,Banner>to        ConcurrentHashMap<String,SerializableForL>).

The banner is a SerializableForL.

+3
source share
1 answer

You cannot assign Map<String,Banner> Map<String,SerializableForL> , even if BannerimplementsSerializableForL .

, SerializableForL, :

ConcurrentMap<String, ? extends SerializableForL> test = DBStore.Cache.get(tag);
+4

All Articles