Inspectopedia 2025.2 Help

Sorted collection with non-comparable elements

Reports construction of sorted collections, for example TreeSet, that rely on natural ordering, whose element type doesn't implement the Comparable interface.

It's unlikely that such a collection will work properly.

A false positive is possible if the collection element type is a non-comparable super-type, but the collection is intended to only hold comparable sub-types. Even if this is the case, it's better to narrow the collection element type or declare the super-type as Comparable because the mentioned approach is error-prone.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

SortedCollectionWithNonComparableKeys
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Probable bugs

The inspection also reports cases when the collection element is a type parameter which is not declared as extends Comparable. You can suppress the warnings on type parameters using the provided option (for example, to keep the API compatibility).

New in 2018.3

Inspection options

Here you can find the description of settings available for the Sorted collection with non-comparable elements inspection, and the reference of their default values.

Don't report non-comparable type parameters

Default value:

Not selected

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection SortedCollectionWithNonComparableKeys

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025