Note that “for each” iteration must be avoided:
long t = 0; for(long x: s) t += x;In the loop above, boxing and unboxing is happening (even if your IDE does not report it). In some cases, a solution is to use a type-specific forEach():
Or we can use fastutil's type-specific version of Java 8's streams:
long t = m.longStream().sum();