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

  • Home
  • SEARCH
  • 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 8267765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:39:00+00:00 2026-06-08T05:39:00+00:00

I created the following Test class: public class GenericTest { public static class A

  • 0

I created the following Test class:

public class GenericTest {

    public static class A implements Serializable {

    }

    public static class B implements Serializable {

    }

    public static void main(String[] args) {
        // Error: Type mismatch: cannot convert from List<capture#1-of ? extends Serializable> to List<GenericTest.A>
        //List<A> aList = getInfo().get("A");
        //List<B> BList = getInfo().get("B");

        // Warning: Type safety: Unchecked cast from List<capture#1-of ? extends Serializable> to List<GenericTest.A>
        List<A> aList = (List<A>)getInfo().get("A");
        List<B> BList = (List<B>)getInfo().get("B");
    }

    public static Map<String, List<? extends Serializable>> getInfo() {
        Map<String, List<? extends Serializable>> infoMap = new HashMap<String, List<? extends Serializable>>();

        List<A> aList = new ArrayList<A>();
        List<B> bList = new ArrayList<B>();

        try {
            aList.add(new A());
            infoMap.put("A", aList);

            bList.add(new B());
            infoMap.put("B", bList);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        return infoMap;
    }    
}

Is there a better way to go about this to avoid casting and suppressing the unchecked warning? I have been told using casting almost defeats the purpose of using Generics in the first place. Is there a problem with this, or a “safer” way to go about doing it?

  • 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-06-08T05:39:01+00:00Added an answer on June 8, 2026 at 5:39 am

    No, there is no way to help this situation. You have a single map with two kinds of values (this is called a heterogeneous map) and the type system cannot express that. You must downcast without type safety. Either that, or completely redesign to keep these two kinds of objects in two separate structures.

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

Sidebar

Related Questions

public class RunScript { public static void main(String[] args) { System.out.println(Hello World); } }
I created a following class within a namespace Global namespace Global { public static
Imagine the following classes: class A { public string Test {get; set;} } class
Given the following Java code: public class Test { static private class MyThread extends
I have created the following test server using java: import java.io.*; import java.net.*; class
Consider following class class test { public: test(int x){ cout<< test \n; } };
I created a file test.class.php containing the following code in a LAMP environment: <?php
In the following code I've created a JTable in class main and I'm starting
Following classes are created, no interface/abstract class for now: Test - for creating test
I have following code class Test { public: int &ref; int a; Test(int &x)

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.