I'm attempting to define a method putIfGreaterThan()
for my new Map
class (given a key it replaces the old value with the new value only if the new value is greater than the old value).
I understand I could accomplish this either via composition (by having a private final Map<String, Double> map;
in new class and then passing a Map
to constructor) or by implementing the Map
interface in my new class and passing a Map
to the constructor (although I'm not sure which approach is superior).
My main problem is that I need to be able to call the method putIfGreaterThan()
on <String, Double>
and <String, Integer>
but not on <String, String>
(bec it doesn't make sense to call it on <String, String>
). If I use generics ( <K, V>
) the client can pass a <String, String>
which is not allowed. On the other hand if I allow a Double
I will not be able to pass an Integer
or vice versa. How can I define a method to allow either Integer or Double but not String?
Note: I'm unable to define two constructors (one for Double and one for Integer) bec I get the error: Erasure of method XX is the same as another method in type XX .