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

Publié le: 07 January 2026
sur la chaîne: 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


Sur cette page du site, vous pouvez voir la vidéo en ligne Comparable vs Comparator in Java | Sorting Objects | Lambda Expressions Explained durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur ShashCode 07 January 2026, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 2,038 fois et il a aimé 109 téléspectateurs. Bon visionnage!