A specialized primitive collection stores primitive values directly and accepts primitive arguments without boxing. This avoids mismatches such as comparing an Integer with a Short during collection searches. section 8.1
Use specialized collections to avoid common boxed-primitive pitfalls:
- Prefer libraries such as FastUtil for better memory use, performance, and primitive-valued operations. section 8.1
- Test both successful and unsuccessful search paths, including removal operations. section 8.1
- Avoid Byte and Short collections when specialized alternatives are unavailable; an Integer collection may not cost more or perform worse. section 8.1
void processList(List<Short> values, short pivot) {
if (values.contains(pivot)) { #1
…
} else if (values.contains(pivot + 1)) { #2
…
}
}
A short-valued list demonstrates how searches involving a primitive pivot and pivot + 1 can expose boxing-sensitive behavior. section 8.1
