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.

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

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.16690

Last modified: 29 April 2024