I am trying to define a method putIfGreaterThan()for my new class Map(taking into account the key, it replaces the old value with the new value only if the new value is greater than the old value).
I understand that I could accomplish this either through composition (having private final Map<String, Double> map;in a new class and then passing the constructor Mapto the constructor) or implementing the interface Mapin my new class and passing to the Mapconstructor (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>(because it makes no sense to call it <String, String>), If I use generics ( <K, V>), the client can pass <String, String>, which is not allowed. On the other hand, if I allow Double, I will not be able to transmit Integeror vice versa. How can I define a method that allows either Integer or Double, but not String?
Note. I cannot define two constructors (one for Double and one for Integer) because I get an error: Erasure of method XX is the same as another method in type XX.
source
share