youngMYN
youngMYN

Reputation: 1

How to initialize a generic class that implements Comparable

The essence is quite simple: I have a container class that contains a Map with elements that implement the Compable interface. At the same time, the container class itself also implements the Compable interface. It looks like this:

public class Unit<V extends Comparable<? super V>> implements Comparable<Unit<V>>{

    @Setter
    private Map<String, V> params;

    @Override
    puplic int compareTo(Unit<V> u){
        ...
    }
    ...
}

Right now my calling code looks like this and it works:

Unit unit1 = new Unit<>;
Map<String, Comparable> params = new HashMap<>();
params.put("First", 123);
params.put("Second", "ABS");
params.put("Third", 'a');

unit1.setParams(params);

Is there some way to specify specific types in the calling code? I want to avoid "Raw use of parameterized class"

I tried to use '?' characters, but then I would have to change the Unit class and I would not be able to call the compareTo() method

Upvotes: 0

Views: 59

Answers (0)

Related Questions