Interview Question | Comparable vs Comparator in Java
Вставка
- Опубліковано 19 січ 2018
- Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
Instagram : / navinreddyofficial
Linkedin : / navinreddy20
TELUSKO Android App : bit.ly/TeluskoApp
Discord : / discord
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
- Collection.sort method to sort the List
- When to use Comparable and when to use Comparator
- compareTo() method
- How to use comparable
- How to use Comparator
#java #interview
Java and Spring Framework For beginners with Spring Boot : - bit.ly/3LDMj8D
Java Tutorial for Beginners (2023) :- bit.ly/3yARVbN
Editing Monitors :
amzn.to/2RfKWgL
amzn.to/2Q665JW
amzn.to/2OUP21a.
Subscribe to our other channel:
Navin Reddy : ua-cam.com/channels/xmk.html...
Telusko Hindi :
ua-cam.com/channels/itz.html...
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
www.telusko.com/contactus - Наука та технологія
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.
@@AK-rx5yp : I have on doubt when we can write our own logic with compare To() method if we are implementing Comparable why should we use Comparator interface?
What is difference between comparable and comparator
@@naksnaz using comparable you can give only one logic and its locked.. Locked meaning you have given that one logic to sort in your class which implements comparable. In this case sort by RAM.
comparator comes handy when you have sort by multiple fields. You can use comparator to sort by BRAND and then again use that result to sort by PRICE.
Basically to have multiple sorting. Which is not possible using comparable.
@@naksnaz change default sort() work, means by default it's sorted by ram if want to sorted by other parameter need to use comprator.
@@sathyalalithanjaliavadhanu835 comparable interface basically work for sort objects by default parameter bt using comparator we can sort objects according to our choice parameter.
This guy is simply amazing. Hats off to his teaching abilities !!
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)
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!
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
I'm here to learn for my exams, nobody explained it so simple like you thanks!
Clearly explained, this is wt exactly I was expecting for😍
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!
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!
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.
here inside the anonymous class we wrote compare() right? does it have to be compare() or can the be different like comparing() or comparePrice() or something?
You are awesome... You teach in such a manner that anyone can pick. Thank you so much
are you crazy? this was way too fast and he didnt explain what he did via autogenerate. thats not good teaching imho.
@@habdochkeineahnung his older videos explain all that. The title says interview questions where he assumes you already know the basics and are preparing for the interview.
The way you teach, makes one understand each topic so easily. Thank you Sir.
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
Thank you, your video on comparator and comparable made the concept so easy to understand. Simple and straightforward, thanks once again.
Awesome. Thanks for sharing your excellent knowledge !
You made the concept so easy to understand.thank u sir
The Simplest and the best explanation about "Comparable vs Comparator"
Your tutorial is perfect. Thanks.
More CS professors should watch your videos to learn how to explain these concepts better. Thank you so much!
Thank you, clear and beautiful explanation.
Brilliant explanation, so important to know both approaches - i was not aware of the second one before - thanks a million!! 😆
Your teaching skills are amazing!!! Thank you so much for making such valuable videos
Hi Naveen, I thoroughly enjoyed the way you have put all the facts before us to understand this with crystal clarity!
Thanks!
I enjoy your tutorials! Thank you, Navin!
Hi Navin, thanks so much for making this video. It is really clear and logical and gives you the understanding of WHY we use each one. compareTo(myUniversityProfessor) who just writes the syntax on the board and shrugs his shoulders. Brilliant!
Pretty cool man, that compareTo().
Your explanation is really superb. I was able to quickly grasp why these interfaces are used & where used from this video. Thanks so so so much, Navin.
Nobody does the job of using code to explain concepts... Great Naveen.. Thanks For The Video..
This was such a helpful video. Thank you so much for making it.
Great way of explaining, understood in one go
Simple..crisp...to the point! and concepts made easy!
Excellent. Awesome and no one likely to have problem understanding. Thank You.
Nice.. your explanation is very simple.. I went through other explanations but yours is easy to understand..
Clearly explained. Thanks a lot
Wow, plain and clear, thanks Sir 👍
So well explained! Really appreciate... 👍
Thank you, I got clear picture about the comparable and comparator
Mind-blowing explanation. Thank you
you are awesome! completely understood the concepts. before thins wherever I was reading about comparable and comparator, I was getting more confused.
Nice explanation.... Thank you ....
Such picturesque explanation 👌
I love watch your java tutorials Sir :) can you make a tutorial for CPU scheduling having a gantt chart? If you do Sir, i will influence more of my classmates to watch your Knowledgeable tutorials. Thank You so much!
thank you sir, this video was crystal clear for me
Very well explained ..!!
Thank you so much..!!
Naveen sir i was having lot of confusion with this concepts thanks for exlpaining it clearly with example
explained it, in quite and easy manner... Thanks a lot.
damn good explanation... Much needed thanks sir :)
This guy is the man. Thanks a lot.
You explanation are always superb. Thanks you.
Excellent, Well done!
I have never seen a teacher like you sir....you are damn awesome
thank you, I love your explanation
Thanks for the video!
Very nice video on just the topics you are wondering about. Teluko, tussi great ho ji!
Perfect.
i was roaming and watching different videos about this, Finally found the perfect one !!
Excellent video Telusko, I've now subbed to your channel!!
What a great video. Understood the concept clearly
Great explanation ! Keep on !
It's amazing... Great explanation..
Nice information.Thanks a lot ....
Mr. Perfect @Navin Reddy... Loved your teaching style...Awesome!!!
Thank you so much, its very clear and precise
crystal clear explanation! thanks!
I love your explanations!
Consice and informative,great video
Very clean and clear way of explaining...no probable doubts you are leaving for your aliens...!!👍👍
You are awesome... You teach in such a manner that anyone can pick
Thank you so much sir, this was the easiest explanation I could fine online.
thanks for the video !! greetings from Argentina !!
How can someone teach so well. You are awesome.
Thank you so much! I was totally lost on how comparators worked until this video.
bless you man, you make it so easy
Clean, efficient explanation. thanks
You saved my midterm! Thank you sir!
SO GOOD Thank you!!
The explanation was amazing. Would be glad if you could deep dive a bit into the functioning of that compareTo and compare function.
Nice Explanation...
Very well explained, thank you.
You are awesome!!! Thank you so much!!!
Excellent video, very well explained, ty so much!
Simply Amazing..!!! Thank you so much
Sir your great .. you have given good teaching methodology.....thank you so much
thanks for explaining in such a nice way
Thanks a lot navin. This is really helpful....
Dude that's a perfect explanation, thank you.
Thanks, well explained
You made it easy Sir. Thank you
Best explanation I've found, thanks
Thank you. Very informative video
great explanation! thank you
here inside the anonymous class we wrote compare() right? does it have to be compare() or can the be different like comparing() or comparePrice() or something?
Great explanation!! Thank you sir!!
Great explanation and examples!
Excellent way to describe when to go for Comparable vs Comparator
Amazing tutorial man, thanks.
awesome video, thank you Telusko!
Great explanation in a simplest way possible.
Easy simple and crisp ❤thanks
Thanks so much for the great explanation.
Bro I love you man haha I was so confused on the differences between comparable and comparator.
You are amazing! it's crystal clear. as i am trying to solve one of the problem in geeksforgeeks, I couldn't get good info on comparator. This one clarifies.