Comparison Method Violates Its General Contract! (Part 2) by Stuart Marks

3,307 views · Published 8 November 2017 · 30:24 · Indexed 1 November 2026

Channel: Devoxx · 2017 · Science & Technology

Watch on YouTube

Why does sorting sometimes throw this exception? What does it mean? And why does it sometimes occur and sometimes work fine?

The answer is in the Comparator interface. A Comparator has one task: to compare two objects. What could be simpler? There are many ways to get it wrong. Writing a complex Comparator is tedious and error-prone. A single mistake can lead to the dreaded error.

In this talk, we take a detailed look at Comparators and examine how to write a correct Comparator. We cover several common pitfalls. We also cover some utility methods introduced in Java 8 that make it easy to compose complex Comparators from simpler ones. These enable you to write correct, sophisticated Comparators much more quickly and easily.

# Stuart Marks
Stuart Marks is a Principal Member of Technical Staff in the Java Platform Group at Oracle. He is currently working on a variety of JDK core libraries projects, including Collections, Lambda, and Streams, as well as improving test quality and performance. As his alter ego "Dr Deprecator" he also works on the Java SE deprecation mechanism. He has previously worked on JavaFX and Java ME at Sun Microsystems. He has over twenty years of software platform product development experience in the areas of window systems, interactive graphics, and mobile and embedded systems. Stuart holds a Master's degree in Computer Science and a Bachelor's degree in Electrical Engineering from Stanford University.

More from this channel