Interview Question | Comparable vs Comparator in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

For More Queries WhatsApp or Call on : +919008963671

In this video we will see :
- Comparator and Comparable in Java
- Difference between Comparator and Comparable
- Example of Comparator and Comparable
- Create getters and setters
- toString() method
- Creating List and adding values in List
- When to use Comparable and when to use Comparator
- compareTo() method
- How to use comparable
- How to use Comparator

#java #interview

Editing Monitors :

Subscribe to our other channel:
Telusko Hindi :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Рекомендации по теме
Комментарии
Автор

Perfect...
1. for known datatypes like String, Int the classes have already implemented Comparable, so you don't need to bother about it.
2. For your own Objects you need to implement Comparable to make the sort() work, which is basically mimicking the strategy for point 1.
3. If you want to change your sort logic implemented by your Comparable interface you need to use Comparator, for either known datatypes or your own classes.
4. You can make the sort() for your own classes work if you implment Comparator interface, whether you have implamanted Comparable or not.

saby
Автор

This guy is simply amazing. Hats off to his teaching abilities !!

raragam
Автор

Comparator is a functional interface, so you can pass a logic directly by lambda through argumen list like that:

Collections.sort(laps, (lap1, lap2)=>if lap1>la2 return 1 else return -1)

filip
Автор

I'm here to learn for my exams, nobody explained it so simple like you thanks!

steviet
Автор

Wow! Just wow man! You are the MVP!
There is no other place on the internet where I could learn these concepts without getting lost. You made it look so simple. Thank you very much!!! You are very talented in teaching!

axeedo
Автор

Normally I don't leave comments on these types of video's, but I must say that this video is very helpfull.
I have an exam due tomorrow and just wanted a quick refresh of one of the subjects.

Thank you!

bqlbkne
Автор

i have been searching for a good source of comparison between these two for days. Finally one video which explains the difference perfectly.
Thank you so much Navin

zombnie
Автор

Clearly explained, this is wt exactly I was expecting for😍

hasmi_intentart
Автор

9:55 I just wanted to add that sorting based on Price can also be done using Comparable, if we override compareTo method to compare prices. Also, a major difference is that we can have multiple comparators in a class, but only one comparable. I experimented and tried to create a second compareTo method in my class using anonymous class, but failed because 'this' keyword wouldn't have scope inside the anonymous class. That's when I realized that compareTo actually compares the present instance of a class to the passed object, which is why there can be only one in a class. compare method has no such restrictions, and it doesn't even have to be in the same class, because it uses two distinctly defined objects.

AnirudhSharma
Автор

The way you teach, makes one understand each topic so easily. Thank you Sir.

rudrakshjhalani
Автор

Thank you very much, you have explained important concept so simply, and have made it very easy for the listeners to understand it. Thanks once again!

purnimadas
Автор

You are awesome... You teach in such a manner that anyone can pick. Thank you so much

AshishBurnwal
Автор

Naveen, you're just great!!
Brilliant and hilariously easy-to-understand explanation.
Not everyone has got the ability to teach things in a simple manner as you do..Einstein of Java I must say

dnyaneshtathode
Автор

More CS professors should watch your videos to learn how to explain these concepts better. Thank you so much!

hughsoward
Автор

Awesome. Thanks for sharing your excellent knowledge !

jatinderarora
Автор

Thank you, your video on comparator and comparable made the concept so easy to understand. Simple and straightforward, thanks once again.

purnimadas
Автор

You made the concept so easy to understand.thank u sir

AhmedVlogs
Автор

Thank you, clear and beautiful explanation.

gongjiaji
Автор

The Simplest and the best explanation about "Comparable vs Comparator"

alperozdamar
Автор

Nobody does the job of using code to explain concepts... Great Naveen.. Thanks For The Video..

maneaniket