liveBook · Manning

Specialized primitive collection

As explained in 100 Java Mistakes and How to Avoid Them

A specialized primitive collection is a collection implementation that stores primitive values directly and provides operations accepting those values without boxing.

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

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Defines direct primitive storage and unboxed operations, then stops at library-level guidance without implementation details.
Examples
Uses List<Short> searches involving a short pivot and pivot + 1 as the focused example.
What it assumes
Distinguishes primitive values from boxed types such as Integer, Short, and Byte when reasoning about collection methods.
What you can do after
Apply it via FastUtil to avoid boxing and reduce memory or performance costs.
How it connects
Connects primitive collections with boxing, collection searches, removals, and static analysis.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.