Issue with declaration of Map<String,Class<? extends Serializable>>

Ivan

Java provides me by <? extends class> a way of filtering the java classes that you can use to build in this case the new HashMap, for example:

I can do that:

Map<String,? extends Serializable> map1 = new HashMap<String,String>();

It is correct, because String implements Serializable, so the compiler let me do that.

But when i try to do it:

Map<String,GenericClass<? extends Serializable>> map2 = new HashMap<String, GenericClass<String>>();

Being the GenericClass as it:

public class GenericClass<T>
{
.
.
.
}

The compiler throw an error saying:

Type mismatch: cannot convert from HashMap<String,GenericClass<String>> to Map<String,GenericClass<? extends Serializable>>

I would like to know, what is happen?

Maybe the compiler cannot detect the extends class being part of a generic type.

Paul Bellora

You would need to use the following:

Map<String, ? extends GenericClass<? extends Serializable>> map2 =
        new HashMap<String, GenericClass<String>>();

Nested wildcards are much different from top-level wildcards - only the latter perform wildcard capture. As a result, HashMap<String, GenericClass<String>> is considered inconvertible to Map<String, GenericClass<? extends Serializable>>, because GenericClass<? extends Serializable> is a concrete type argument (and because generics aren't covariant).

See this post for further information on nested wildcards: Multiple wildcards on a generic methods makes Java compiler (and me!) very confused

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Is class member declaration not a statement?

분류에서Dev

오류 : [Dagger / MissingBinding] Map <Class <? Extends ViewModel>, Provider <ViewModel >>은 @ Provides-annotated 메서드 없이는 제공 될 수 없습니다.

분류에서Dev

Serializable class with unserializable injected member in JAVA

분류에서Dev

"syntax error, unfinished class declaration"

분류에서Dev

Java Class extends Abstract class but must have static method

분류에서Dev

Autowired not working if used inside class that extends Spring security class

분류에서Dev

Android: Can't call new activity from class that extends Thread

분류에서Dev

Where is declaration of invoke method in Delegate class?

분류에서Dev

Openlayers 3 : Issue in Changing Extents of the Map

분류에서Dev

map <string, class> 사용 및 연결

분류에서Dev

Replace element in string issue

분류에서Dev

class <T extends class>를 확장하는 방법

분류에서Dev

What is the difference between private and private[Class] declaration in scala?

분류에서Dev

Drools variable declaration in condition depending on class type using "not"

분류에서Dev

Box iOS SDK - Receiver 'BoxSearchRequestBuilder' for class message is a forward declaration

분류에서Dev

Map to String/String to Map conversion in Groovy

분류에서Dev

properties = Map [String, Map [String, DProperty]] ()

분류에서Dev

Android: Redundancy issue in datahelper class

분류에서Dev

Issue Creating Test Class For Trigger

분류에서Dev

유형의 개체를 JMS 메시지로 변환 할 수 없습니다. 지원되는 메시지 페이로드는 문자열, 바이트 배열, Map <String,?>, Serializable 객체입니다.

분류에서Dev

코드 스 니펫 Class Abc <R extends XYZ> extends DAO는 무엇을 의미합니까?

분류에서Dev

Scala에서 "case class extends trait"는 무엇을 의미합니까?

분류에서Dev

Amazon Elastic Map Reduce : Command Line Tools installation Issue?

분류에서Dev

String comparison issue in bash script

분류에서Dev

doGet and doPost String = null issue

분류에서Dev

String matching regex syntax issue

분류에서Dev

의 뜻 ? Map <String,?>

분류에서Dev

scala 3 매크로 : 매크로에서 Map [String, Class [Any]] 반환

분류에서Dev

How to resove the issue unable to resolve class XSSFWorkbook?

Related 관련 기사

  1. 1

    Is class member declaration not a statement?

  2. 2

    오류 : [Dagger / MissingBinding] Map <Class <? Extends ViewModel>, Provider <ViewModel >>은 @ Provides-annotated 메서드 없이는 제공 될 수 없습니다.

  3. 3

    Serializable class with unserializable injected member in JAVA

  4. 4

    "syntax error, unfinished class declaration"

  5. 5

    Java Class extends Abstract class but must have static method

  6. 6

    Autowired not working if used inside class that extends Spring security class

  7. 7

    Android: Can't call new activity from class that extends Thread

  8. 8

    Where is declaration of invoke method in Delegate class?

  9. 9

    Openlayers 3 : Issue in Changing Extents of the Map

  10. 10

    map <string, class> 사용 및 연결

  11. 11

    Replace element in string issue

  12. 12

    class <T extends class>를 확장하는 방법

  13. 13

    What is the difference between private and private[Class] declaration in scala?

  14. 14

    Drools variable declaration in condition depending on class type using "not"

  15. 15

    Box iOS SDK - Receiver 'BoxSearchRequestBuilder' for class message is a forward declaration

  16. 16

    Map to String/String to Map conversion in Groovy

  17. 17

    properties = Map [String, Map [String, DProperty]] ()

  18. 18

    Android: Redundancy issue in datahelper class

  19. 19

    Issue Creating Test Class For Trigger

  20. 20

    유형의 개체를 JMS 메시지로 변환 할 수 없습니다. 지원되는 메시지 페이로드는 문자열, 바이트 배열, Map <String,?>, Serializable 객체입니다.

  21. 21

    코드 스 니펫 Class Abc <R extends XYZ> extends DAO는 무엇을 의미합니까?

  22. 22

    Scala에서 "case class extends trait"는 무엇을 의미합니까?

  23. 23

    Amazon Elastic Map Reduce : Command Line Tools installation Issue?

  24. 24

    String comparison issue in bash script

  25. 25

    doGet and doPost String = null issue

  26. 26

    String matching regex syntax issue

  27. 27

    의 뜻 ? Map <String,?>

  28. 28

    scala 3 매크로 : 매크로에서 Map [String, Class [Any]] 반환

  29. 29

    How to resove the issue unable to resolve class XSSFWorkbook?

뜨겁다태그

보관