Interview Question | Comparable vs Comparator in Java
HTML-код
- Опубликовано: 5 сен 2024
- Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-sp...
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusk...
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 : www.youtube.co....
Telusko Hindi :
www.youtube.co....
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
www.telusko.com...
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 !!
I'm here to learn for my exams, nobody explained it so simple like you thanks!
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!
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
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.
Clearly explained, this is wt exactly I was expecting for😍
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?
Nobody does the job of using code to explain concepts... Great Naveen.. Thanks For The Video..
The Simplest and the best explanation about "Comparable vs Comparator"
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
Brilliant explanation, so important to know both approaches - i was not aware of the second one before - thanks a million!! 😆
More CS professors should watch your videos to learn how to explain these concepts better. Thank you so much!
Your teaching skills are amazing!!! Thank you so much for making such valuable videos
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().
Mr. Perfect @Navin Reddy... Loved your teaching style...Awesome!!!
Awesome. Thanks for sharing your excellent knowledge !
Hi Naveen, I thoroughly enjoyed the way you have put all the facts before us to understand this with crystal clarity!
Thanks!
Thank you, your video on comparator and comparable made the concept so easy to understand. Simple and straightforward, thanks once again.
By using Comparable we can sort the objects based on any data member. For example, lets say we have an Author class has data members: Author name, book name and author age, now if we want to sort the objects based on any of the data members then we can use Comparable but what if we want to have multiple sort choices and we can sort objects based on any choice, this can be done using Comparator interface, we can create as many Comparator as we want and then we can call Collections.sort on one or more Comparator
Simple..crisp...to the point! and concepts made easy!
Nice.. your explanation is very simple.. I went through other explanations but yours is easy to understand..
His explanation is so easy that you are bound to learn. If you can't learn from him, then coding is not for u
"Do simple things, simple things are great" - This Video
Best tutorial that made so complex logic seem so simple
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. 👍👍
You saved my midterm! Thank you sir!
Thank you so much! I was totally lost on how comparators worked until this video.
The way you teach, makes one understand each topic so easily. Thank you Sir.
Comparator custInt = (a,b) -> a>b?1:-1; will work. If it is in the same line this works.
Excellent way to describe when to go for Comparable vs Comparator
You explanation are always superb. Thanks you.
Mashallah, explained very well, hats off sir😊
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.
Excellent. Awesome and no one likely to have problem understanding. Thank You.
The perfect explanation.. you are the java encyclopaedia sir!!
Thanks pal , very useful video. Greetings from Colombia.
First time here and I totally understand why you have 1.57 M subs, you are a real master! awesome explanation.
Perfect.
i was roaming and watching different videos about this, Finally found the perfect one !!
Very nice video on just the topics you are wondering about. Teluko, tussi great ho ji!
sir great .....i m java developer.....your teach way is too good(tussi kmaal krde hunde aa)
This guy is the man. Thanks a lot.
Crystal clear in short time
Bro I love you man haha I was so confused on the differences between comparable and comparator.
thank you sir, this video was crystal clear for me
Clear cut explanation, thank you sir
you are awesome! completely understood the concepts. before thins wherever I was reading about comparable and comparator, I was getting more confused.
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!
Simply Amazing..!!! Thank you so much
You made the concept so easy to understand.thank u sir
Thank you so much sir, this was the easiest explanation I could fine online.
crystal clear explanation! thanks!
Great explanation in a simplest way possible.
Ur way of teaching is very lucid and impressive too. Please make some videos on java 8 and 9.
Great explanation and examples!
Easy simple and crisp ❤thanks
Easily understood thanks bro
Your tutorial is perfect. Thanks.
best explanation available on youtube
Perfect explanation.Thanks
The explanation was amazing. Would be glad if you could deep dive a bit into the functioning of that compareTo and compare function.
I have never seen a teacher like you sir....you are damn awesome
Thanks for good explanation sir !
Awesome Explanation
very clear and precise.. thanks a lot
Perfect bro..🙂🙂 you are good teacher.
Amazing tutorial man, thanks.
Excellent video, very well explained, ty so much!
You are awesome... You teach in such a manner that anyone can pick
awesome video, thank you Telusko!
Very clean and clear way of explaining...no probable doubts you are leaving for your aliens...!!👍👍
Such picturesque explanation 👌
Sir your great .. you have given good teaching methodology.....thank you so much
Dude that's a perfect explanation, thank you.
For Final and Abstracr classes we can use Comparator becoz we cannot make them comparable .. Wonderful Explanation
So well explained!! Thank you so much ...
Yes, I enjo.. I mean understood what is comparable.
Amazingly clear explanation. Impossible not to get it !!! I rarely subscribe to anyone, but for this time LIKE and SUBSCRIBE just to thank you for this video! :)
Naveen sir i was having lot of confusion with this concepts thanks for exlpaining it clearly with example
Oh we have to mention explicitly, 👍
Cool...nice contents
wow very well explained sirr :) tq
Perfect explanation 👍
bless you man, you make it so easy
great explanation! thank you
Excellent explanation.Thanks :) I am new subsscriber
Underrated. After watching the whole internet you are the best hands down
By whom it's underrated? Or do you only practice your ass kissing skills? But yeah, in the end you got your ass kissing heart badge xD
Well explained
thank you sir my big question is clear
And for those who still don't know, what Telusko did for the code of the compareTo() method.
This code here is the equivalent and much shorter to type:
@Override
public int compareTo(Laptop lap2)
{
return Integer.compare(this.ram, Laptop.ram);
}
You are far better than my professors,
Thank you sir 🤗
So well explained! Really appreciate... 👍
explained it, in quite and easy manner... Thanks a lot.
Great
Very nice, learnt a lot.
Nice explanation.
nice work big like
I enjoy your tutorials! Thank you, Navin!