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?
@@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.
@@sathyalalithanjaliavadhanu835 comparable interface basically work for sort objects by default parameter bt using comparator we can sort objects according to our choice parameter.
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!
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!
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
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?
@@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.
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)
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
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!
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.
This guy is simply amazing. Hats off to his teaching abilities !!
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.
I'm here to learn for my exams, nobody explained it so simple like you thanks!
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!
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!
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
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.
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)
Clearly explained, this is wt exactly I was expecting for😍
Nobody does the job of using code to explain concepts... Great Naveen.. Thanks For The Video..
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
The Simplest and the best explanation about "Comparable vs Comparator"
Brilliant explanation, so important to know both approaches - i was not aware of the second one before - thanks a million!! 😆
Bro I love you man haha I was so confused on the differences between comparable and comparator.
Mr. Perfect @Navin Reddy... Loved your teaching style...Awesome!!!
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().
More CS professors should watch your videos to learn how to explain these concepts better. Thank you so much!
Simple..crisp...to the point! and concepts made easy!
This guy is the man. Thanks a lot.
Your teaching skills are amazing!!! Thank you so much for making such valuable videos
You saved my midterm! Thank you sir!
Awesome. Thanks for sharing your excellent knowledge !
You made the concept so easy to understand.thank u sir
Cool...nice contents
Great
The way you teach, makes one understand each topic so easily. Thank you Sir.
nice work big like
Well explained
Thank you, your video on comparator and comparable made the concept so easy to understand. Simple and straightforward, thanks once again.
crystal clear explanation! thanks!
Crystal clear in short time
Great explanation and examples!
Hi Naveen, I thoroughly enjoyed the way you have put all the facts before us to understand this with crystal clarity!
Thanks!
Your tutorial is perfect. Thanks.
Mashallah, explained very well, hats off sir😊
best explanation available on youtube
Awesome Explanation
Perfect explanation.Thanks
Great explanation in a simplest way possible.
Nice.. your explanation is very simple.. I went through other explanations but yours is easy to understand..
Perfect explanation 👍
You explanation are always superb. Thanks you.
"Do simple things, simple things are great" - This Video
Amazing video
Great
sir great .....i m java developer.....your teach way is too good(tussi kmaal krde hunde aa)
The perfect explanation.. you are the java encyclopaedia sir!!
Outstanding!
Such picturesque explanation 👌
Best tutorial that made so complex logic seem so simple
Best explanation
Easily understood thanks bro
Amazing explanation
Thank you so much! I was totally lost on how comparators worked until this video.
Great job, thanks.
Easy simple and crisp ❤thanks
awesome video, thank you Telusko!
Superb explanation bro..... Crystal clear
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!
Thanks for the video!
guru ho bhai sahi me tum ,,, thanks much
Nice explanation.
Excellent. Awesome and no one likely to have problem understanding. Thank You.
SO GOOD Thank you!!
Awesome!!
Nicely explained!
Wow! Just wow!
The best explanation.. Thanks a lot
First time here and I totally understand why you have 1.57 M subs, you are a real master! awesome explanation.
I have never seen a teacher like you sir....you are damn awesome
very clear and precise.. thanks a lot
bless you man, you make it so easy
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.
Great. 👍
Very nice video on just the topics you are wondering about. Teluko, tussi great ho ji!
The way you explains is too good. I am learning from last 7-8 years but I didn't get this type of explanation any where else. 👍👍
Excellent way to describe when to go for Comparable vs Comparator
Amazing man, the best explanation by far
great explanation! thank you
Excellent
Excellent video Telusko, I've now subbed to your channel!!
Thanks pal , very useful video. Greetings from Colombia.
Awesome
So well explained! Really appreciate... 👍
Thanks for good explanation sir !
Thank you so much sir, this was the easiest explanation I could fine online.
Perfect.
i was roaming and watching different videos about this, Finally found the perfect one !!
Simply Amazing..!!! Thank you so much
So well explained!! Thank you so much ...
Clear cut explanation, thank you sir
I enjoy your tutorials! Thank you, Navin!
You are awesome... You teach in such a manner that anyone can pick
Perfect bro..🙂🙂 you are good teacher.
Nice information.Thanks a lot ....
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 ....
Dude that's a perfect explanation, thank you.
nice explanation
You made it easy Sir. Thank you