Optimizing your equals() methods with Pattern Matching - JEP Cafe #21
How can you write simple and performant equals() methods for your Java classes: the full tutorial. Usually you do not write your equals() and hashCode() methods yourself. You delegate that to your IDE and forget about it. This JEP Café deep dives into the different solutions offered by Eclipse and IntelliJ, as well as Guava and Apache Commons Lang 3. It compares these solutions and checks the performance they offer on various real-life use cases. It also compares them with the Pattern Matching solution, available from Java 17 and Java 21. ⎯⎯⎯⎯⎯⎯ Chapters ⎯⎯⎯⎯⎯⎯ 0:00 Intro: designing equals() methods 1:13 Generating equals() and hashCode() methods 1:56 What could go wrong with a bad hashCode() method 4:31 Taking a look at the generated equals() methods 7:11 Checking the record and pattern matching implementations 9:33 What is predictive branching, what is the cost of a failing prediction? 12:41 Evaluating the cost of two failing predictions. 14:53 Evaluating the performance of equals() methods on simple data sets 16:07 First data set: all the objects are the same instance 16:46 Second data set: different instances carrying the same state 18:43 Third data set: different objects of the same type 20:41 Fourth data set: objects of different types 22:00 First conclusions on what patterns are the best 22:39 Adding glitches to the data sets 24:41 Results for first data set (same instances) with glitches 25:20 Results for second data set (equal instances) with glitches 26:07 Results for third data set (different instances) with glitches 28:30 Results for fourth data set (different types) with glitches 30:05 What conclusions can you draw from all these benches? 32:33 That's it for today, talk to you soon! ⎯⎯⎯⎯⎯⎯ Resources ⎯⎯⎯⎯⎯⎯ ◦ Pattern Matching for intanceof ➱ https://openjdk.org/jeps/394 ◦ Records ➱ https://openjdk.org/jeps/395 ◦ Record Patterns ➱ https://openjdk.org/jeps/440 ◦ Guava ➱ https://guava.dev/ ◦ Apache Commons Lang 3 ➱ https://commons.apache.org/proper/com... ◦ Eclipse IDE ➱ https://www.eclipse.org/ide/ ◦ IntelliJ IDE ➱ https://www.jetbrains.com/idea/ ◦ JMH on GitHub, with doc and samples ➱ https://github.com/openjdk/jmh ◦ Alexey Shipilev on JMH ➱ • Java Microbenchmark Harness: The Lesser of... ◦ Dev.java ➱ https://dev.java ◦ Inside.java ➱ https://inside.java ◦ JDK 21 ➱ https://openjdk.org/projects/jdk/21 ◦ JDK 17 ➱ https://openjdk.org/projects/jdk/17 ◦ OpenJDK ➱ https://openjdk.org ◦ Oracle Java ➱ https://www.oracle.com/java/ #Java #Java21 #OpenJDK #JDK #JDK21 #Collection #JEPCafe #insidejava

Data Oriented Programming in Java 21, Solving the Countdown game - JEP Cafe #22

1B Rows with the Memory API - JEP Cafe #25

Java 21 new feature: Virtual Threads #RoadTo21

I Was Right About AI

Understanding Java String Hashcode - explained with use cases and examples

Choosing between ArrayList and LinkedList - JEP Cafe #20

China Just Built What TSMC Said Was Impossible

He Once Worked at Subway. At 58, He Solved An "Impossible" Problem

Know Your Java?

The New Java Best Practices by Stephen Colebourne

Java Asynchronous Programming Full Tutorial with Loom and Structured Concurrency - JEP Café #13

How Linear Algebra Powers Machine Learning (ML)

Java 21 New Feature: Sequenced Collections - JEP Cafe #19

首富的黄昏:王健林是怎么得罪习近平的?

Overriding equals and hashCode in Java - 039

The UK's Under-16 Social Media Ban Was Never About Children...

Learn how to write fast Java code with the Vector API - JEP Café #18

Clean Code with Records, Sealed Classes and Pattern Matching by José Paumard

Java 21 API New Features #RoadTo21

