I have a static function with the following signature for generic type T
public static<T> List<T> sortMap(Map<T, Comparable> map)
which should return a list of card keys with some property.
Now I want to pass a generic HashMap of type S
Map<S,Double> map
when calling a static function in a generic class that has a map as a member variable.
I gave an example of the minimum code below.
However, I get an error message ( S and T are both T, but in different areas of my code, i.e. T#1 = T , T#2 = S ):
required: Map<T#1,Comparable> found: Map<T#2,Double> reason: cannot infer type-variable(s) T#1 (argument mismatch; Map<T#2,Double> cannot be converted to Map<T#1,Comparable>)
How to solve this problem? I am surprised that Java does not allow deriving a generic type from a generic type. What structure in Java can be used to work with more abstract reasoning code?
Code
public class ExampleClass<T> { Map<T, Double> map; public ExampleClass () { this.map = new HashMap(); }
source share