Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:24:47+00:00 2026-05-30T00:24:47+00:00

I have the following code: public static<F, S> void xyz() { class Pair<X, Y>

  • 0

I have the following code:

public static<F, S> void xyz() {
  class Pair<X, Y> {}    
  class Role<F> {}

  Map<?, List<Pair<?, ?>>> map = null;
  Role<F> role = null;

  List<Pair<F, S>> result = (List<Pair<F, S>>) map.get(role);
}

Unfortunately, Java complains about the cast on the last line. Why does this happen?

In my option, this is counterintuitive. A cast should be allowed in every place where there’s a possibility of a correct type correspondence.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:24:49+00:00Added an answer on May 30, 2026 at 12:24 am

    “A cast should be allowed in every place where there’s a possibility of a correct type correspondence.” In this case, there is NO possibility of a correct type correspondence.

    An object can never be an instance of both List<A> and List<B>, if A and B are concrete and not the same, even if A is a subtype of B or whatever. For example, you cannot have an object that is both List<String> and List<Object>. There is (conceptually) a specific type parameter for each object. You have to understand this part first.

    Your example is just the same — List<Pair<F, S>> and List<Pair<?, ?>>. For the same reason as with String and Object above, an object that is of type List<Pair<?, ?>> can never also be List<Pair<F, S>>.

    Update: More explanation in case it helps. @Konstantin: If you write Pair<?, ?>, then those type parameters at the top level, which are ? (wildcards), are flexible. Pair<T1, T2> is compatible with Pair<?, ?>. However, in List<Pair<?, ?>>, the ? is not at the top level. The type parameter at the top level is Pair<?, ?>, which is not a wildcard, and thus is not flexible. It doesn’t matter that you happen to have ? deeper down. If you have List<? extends Pair<?, ?>>, then the top level is a wildcard, and is flexible.

    What you probably want is List<? extends Pair<?, ?>>. Perhaps it will help you to consider the difference between List<Pair<?, ?>> and List<? extends Pair<?, ?>>. List<Pair<?, ?>> says that this is a List, and it type parameter is exactly the type Pair<?, ?> (and nothing else, not Pair<T1, T2>, etc.). List<? extends Pair<?, ?>> says that this is a List, and it type parameter is something which is a subtype of Pair<?, ?> (and this includes Pair<T1, T2>).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
i have the following code: public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
I have the following code: public class Test { public static void Main() {
Let's assume I have the following code: public class MainClass { public static void
I have the following code: public class Tests { public static void main(String[] args)
I have the following code: public class boolq { public static void main(String[] args)
Let us have the following code: public class TestGenerics { static <T> void mix(ArrayList<T>
I have the following code snippet: class Cert { public static void main(String[] args)
I have the following code: public class DeserializeAndCompare { public static List<string> IntoXML() {
I currently have the following code: public class Count { public static void countChar()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.