The type of expression i - 1is equal intbecause all operands in an integer arithmetic expression expand at least to int. Set<Short>has add(Short)and remove(Object), therefore remove, casting / autoboxing is not required for the call . Therefore, you are trying to remove s Integerfrom the set Short.
, Set<Number>:
final Set<Number> ns = new HashSet<>();
final short s = 1;
ns.add(s);
ns.add(s+0);
ns.add(s+0L);
System.out.println(ns);
, TreeSet, , ClassCastException, .
, , . Java.