Comparable interface
Comparable is an interface, protocol, or mechanism that specifies whether values of a type can be compared or ordered.
100 Java Mistakes and How to Avoid Them
Comparable is a Java interface whose implementation defines a natural ordering for instances through compareTo.
- How it is used
- Defines natural ordering for Java objects and exposes comparison mistakes that can silently misorder data.
- How deep it goes
- Details sign-based compareTo rules and focused failures, while limiting collection internals to a collision-dependent HashSet case.
- Examples
- Uses boolean wrappers, a name-and-age User record, and mutable StringBuilder values stored in HashSet.
- What you can do after
- Diagnose/debug comparison code by checking signs, equality results, and unsafe mutable or inconsistent collection keys.
Read how 100 Java Mistakes and How to Avoid Them explains Comparable interface →
Clojure: The Essential Reference
The Java Comparable interface defines a natural ordering for instances of a class through its compareTo method.
- How it is used
- Supplies natural ordering to Clojure’s default comparison and sorting of compatible custom objects.
- How deep it goes
- Explains comparison results, compatibility requirements, exceptions, and a worked record implementation without detailing Java interface internals.
- Examples
- Uses distance-ordered point and gas-station records, plus sorted-map keys, to demonstrate ordering.
- What you can do after
- Build it from scratch by implementing `compareTo` on a Clojure record so its instances can be sorted.
Read how Clojure: The Essential Reference explains Comparable interface →
Classic Computer Science Problems in Swift: Essential techniques for practicing programmers
Comparable is a Swift standard-library protocol for types whose values can be ordered relative to one another.
- How it is used
- Supports ranking search nodes so A* can prioritize candidates by combined cost and heuristic.
- How deep it goes
- Defines comparison for a concrete generic Node type but does not survey Comparable requirements beyond the shown operators.
- Examples
- Uses a generic Node whose < operator compares cost plus heuristic and whose == operator compares reference identity.
- What you can do after
- Apply it via Swift operator overloads to rank custom search nodes for priority-based algorithms.
Related concepts
- compareTo methodhas part · The compareTo method compares an object with another value and returns a negative, zero, or positive integer indicating their relative ordering.
- StringBuilderimplemented by · StringBuilder is a mutable character-sequence type designed for efficiently constructing and modifying text through repeated operations.
- compareused by · compare is a Clojure function that returns a negative, zero, or positive integer indicating the relative order of two comparable values.
- type constraintused by · A type constraint is a rule that restricts admissible types or type arguments by requiring specified relationships, structures, properties, or supported operations.
- comparison functionalternative to · A comparison function or strategy compares two values and indicates their relative ordering, and sometimes whether they are equivalent.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


