Comparison Method Violates Its General Contract! (Part 2)
summary
Explains why a buggy Comparator triggers "comparison method violates its general contract": TimSort used by Java 7+ detects inconsistent ordering, so incorrect logic can throw an exception or silently produce wrong order. Shows Java 8 combinators used to build correct comparators (comparingInt/comparingDouble, thenComparing, nullsFirst/nullsLast), demonstrates BigDecimal’s compareTo inconsistent with equals causing TreeSet vs HashSet surprises, and warns against setting java.util.Arrays.useLegacyMergeSort because it hides bugs rather than fixing them.
AI-written summary · about
chapters (10)
- 0:00Start
- 0:18Combinator Methods on the Comparator Interface
- 1:16Combinator Methods
- 2:44Demo
- 6:39Comparing Int
- 8:26Nullpointerexception
- 16:27The Illegal Argument Exception
- 16:42Java Util Arrays Use Legacy Merge Sort
- 19:09A Downstream Comparator
- 24:12Numeric Consistency Properties
related talks
48mCollections.compare(JDK, Eclipse, Guava, Apache...);
01h 50mFree Your Lambdas
47mHow the JVM Optimizes Generic Code - A Deep Dive
50mKnow Your Java
49mJava Collections Puzzlers
46mCollections.compare:JDK, Eclipse, Guava, Apache
50mThe Hidden Art of Thread-Safe Programming: Exploring java.util.con...
28m11 Crazy Things I Didn't Know You Could Do With Java Until I Got...
