I am implementing Polynomial class with generalized type of polynomial coefficients. I have this code:
public class Polynomial<T> {
private HashMap<Integer, T > polynomial;
public Polynomial(T coefficient, Integer index) {
polynomial = new HashMap<Integer, T>();
if (coefficient!=0)
polynomial.put(index, coefficient);
}
public void sum(Polynomial<T> w) {
for (Map.Entry<Integer, T> e : w.polynomial.entrySet()) {
T tmp = polynomial.get(e.getKey());
if(tmp==null)
polynomial.put(e.getKey(), e.getValue());
else {
polynomial.remove(e.getKey());
if (tmp+e.getValue()!=0)
polynomial.put(e.getKey(), tmp+e.getValue());
}
}
}
...
}
which for obvious reasons does not compile. Operators: ==, !=, +, - and * are not defined for generalized type T. From what I know in Java I can't override operators. How can I solve this problem?