0

I have a generic class Class1<K, V> which takes two types. Now I want to maintain a map which maps String to Class1 objects i.e Map<String, Class1> . Can I retrieve the types (K,V) of each Class1 Object while I iterate through Map?

i.e lets think of code as below

Map<String, Class1> map;
map.put("1", Class1<String, Integer> obj1);
map.put("2", Class1<String, Double> obj2);
map.put("3", Class1<Integer, Double> obj3);

Now when I iterate over the map ... Can I somehow get the types(K, V) of each Class1 object??

3
  • 1
    Generally: no. Practically maybe you can infer the types if you code Class1 correctly and create obj1 is created with an explicit String and Integer object or a reference to their types. Commented Aug 31, 2020 at 12:04
  • You should probably read up on type erasure Commented Aug 31, 2020 at 12:07
  • The problem is type erasure, i.e. the runtime doesn't maintain that information. Besides that the code you've posted wouldn't compile so your actual code probably looks differently. As luk2302 hinted at there are some ways to maintain the type information at runtime which depend on what Class1 actually looks like - and they might no be possible in your case (possible solutions: if the types are types of fields try to get them from those, if you can use non-generic sub classes you could use reflection, or pass type references like String.class etc. to the constructor and store those) . Commented Aug 31, 2020 at 12:08

1 Answer 1

1

It's not possible according to JVM specification.

Generic types are available only at compile time. At runtime ther're only Object.

All you have is 2 options:

  1. Add class definition variable to your objects and us it when you need this info.
  2. E.g. for List<Object> as well as for other collections if it's not empty, you can get an item from it and get item.getClass().

P.S. Buy the way, your approach is not correct to solve this issue. You could implement an interface like:

interface TypeConverter {
    boolean isValid(String str);   // call it before to check if current converter valid for required transform or not

    Class1 converts(String str);  // call it to convert 
}

And for each required types you have to create separate converter (this is Strategy Pattern). And your final code could look like this:

final TypeConverter converters = new ArrayList<>();
converters.add(new OneTypeConverter());
converters.add(new TwoTypeConverter());

public static Class1 convert(String str) {
    for(TypeConverter convereter : converters)
        if(converter.isValid(str))
            return converter.convert(str);
  
    throw new RuntimeException("No suitable converter found");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.