I'm learning about generics and I don't know how to resolve a problem.
This is the code:
public abstract class AbstractMapService<T, ID> {
protected Map<ID, T> map = new HashMap<>();
Set<T> findAll(){
return new HashSet<>(map.values());
}
T findById(ID id) {
return map.get(id);
}
T save(ID id, T object){
map.put(id, object);
return object;
}
void deleteById(ID id){
map.remove(id);
}
void delete(T object){
map.entrySet().removeIf(entry -> entry.getValue().equals(object));
}
private Long getNextId() {
return Collections.max(map.keySet()) + 1;
}
}
And this is the error:
max(java.util.Collection<? extends T>) in Collections cannot be applied to (java.util.Set<ID>)
reason: no instance of type variable(s) T exist so that ID conforms to Comparable<? super T>
Can someone explain to me why I get this error and how to resolve it? Thank you!