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 7935843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:02:40+00:00 2026-06-03T22:02:40+00:00

I have a class with a collection of Wildcard Types that is a singleton,

  • 0

I have a class with a collection of Wildcard Types that is a singleton, something like:

public ObliviousClass{

    private static final ObliviousClass INSTANCE = new ObliviousClass();

    private Map<Key, Type<?>> map = new HashMap<Key, Type<?>>();

    public void putType(Key key, Type<?> type){
        map.put(type);
    }

    // returns the singleton
    public static ObliviousClass getInstance(){
        return INSTANCE;
    }

}

I’d like to be able to add different Parameterized types to this collection in client code:

void clientMethod(){
    ObliviousClass oc = ObliviousClass.getInstance();

    Type<Integer> intType = ...
    Type<String> stringType = ...

    oc.putType(new Key(0), intType);
    oc.putType(new Key(1), stringType);
}

Up to this point, as I understand it, everything is ok. But a client also needs to be able to get a Type<?> provided the Key. So a method something like the following would be added to ObliviousClass:

public Type<?> getType(Key key){
    return map.get(key);
}

But in my handy copy of Effective Java, I read:

Do not use wildcard types as return types.

I understand the issue, as the client would have to cast the returned Type<?>. But I really do not want to make ObliviousClass a generic type, ObliviousClass<T>, because then my client code above would not work…

Is there a better design for what I am trying to do?
-My current solution is to provide a static method for the client; something along the lines of:

public static <T> void getType(ObliviousClass instance, Key key, Type<T> dest){
    dest = (Type<T>)instance.getType(key);
}

I searched around, but wasn’t able to find an answer that totally cleared my confusion.

  • 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-03T22:02:42+00:00Added an answer on June 3, 2026 at 10:02 pm

    Here’s a type-safe way to store multiple instances of a given type in a map. The key is that you need to provide a Class instance when retrieving values in order to perform runtime type-checking, because static type information has been erased.

    class ObliviousClass {
    
      private final Map<Key, Object> map = new HashMap<Key, Object>();
    
      public Object put(Key key, Object value)
      {
        return map.put(key, value);
      }
    
      public <T> T get(Key key, Class<? extends T> type)
      {
        return type.cast(map.get(key)); 
      }
    
    }
    

    Usage would look like this:

    oc.put(k1, 42);
    oc.put(k2, "Hello!");
    ...
    Integer i = oc.get(k1, Integer.class);
    String s = oc.get(k2, String.class);
    Integer x = oc.get(k2, Integer.class); /* Throws ClassCastException */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a collection class like this: public class SomeDataCollection : List<ISomeData> { //
I have a class which contains a static collection that can be used across
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
Suppose I have a class with a collection of immutable types, that I would
I have a collection class that inherits from List<T> public class TestCollection : List<Test>
I have a generic collection based type that holds other generic types: class Collection<T>
I have a collection class that holds lots of different types of data in
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
I have created a custom collection class that I am trying to bind to

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.