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

Publicado em: 07 January 2026
no canal de: 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


Nesta página do site você pode assistir ao vídeo on-line Comparable vs Comparator in Java | Sorting Objects | Lambda Expressions Explained duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário ShashCode 07 January 2026, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2,038 vezes e gostou 109 espectadores. Boa visualização!