Inspectopedia 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

Not selected

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024