Comparable vs Comparator in Java | Sorting Objects | Lambda Expressions Explained

Veröffentlicht am: 07 January 2026
auf dem Kanal: ShashCode
2,038
109

Lecture Resources
https://github.com/Tiwarishashwat/Jav...

In this lecture, we will deeply understand Comparable and Comparator in Java, which are used to compare and sort objects of user-defined classes.

When working with collections like ArrayList, TreeSet, or Collections.sort(), Java needs a comparison logic — and that’s where Comparable and Comparator come into play.

Topics covered in this video:

Why Comparable and Comparator are needed

Comparable interface in Java

Natural ordering

compareTo() method

Return values and sorting logic

Comparator interface in Java

Custom sorting logic

compare() method

Sorting objects using:

Comparable

Comparator with separate class

Comparator using anonymous inner class

Sorting tricks using subtraction (first - second, second - first)

Multi-level sorting (marks + roll number)

Lambda expressions in Java (Java 8+)

Comparator using Lambda

Cleaner and shorter syntax

With multiple real-world examples and step-by-step explanation, this lecture will help you master object sorting, a very important topic for Java interviews and DSA problems.

This video is part of my Java + DSA course by ShashCode, built for:

Java beginners

College students

Placement & interview preparation

Strong Java Collections fundamentals

👉 Watch till the end to clearly understand when to use Comparable vs Comparator.

Timestamp:
0:00 - Compare Objects
2:30 - Comparable interface
8:43 - Comparator interface
20:38 - Lamba Expression
25:21 - Outro


Auf dieser Seite können Sie das Online-Video Comparable vs Comparator in Java | Sorting Objects | Lambda Expressions Explained mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer ShashCode 07 January 2026 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,038 Mal angesehen und es wurde von 109 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!