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...

Комментарии • 364

  • @saby8765
    @saby8765 6 лет назад +212

    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.

    • @naksnaz
      @naksnaz 4 года назад +3

      ​@@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?

    • @sathyalalithanjaliavadhanu835
      @sathyalalithanjaliavadhanu835 4 года назад

      What is difference between comparable and comparator

    • @showdank
      @showdank 4 года назад +11

      @@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.

    • @altamashalisheikh
      @altamashalisheikh 3 года назад +1

      @@naksnaz change default sort() work, means by default it's sorted by ram if want to sorted by other parameter need to use comprator.

    • @altamashalisheikh
      @altamashalisheikh 3 года назад +5

      @@sathyalalithanjaliavadhanu835 comparable interface basically work for sort objects by default parameter bt using comparator we can sort objects according to our choice parameter.

  • @raragam
    @raragam 4 года назад +61

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

  • @steviet5246
    @steviet5246 5 лет назад +20

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

  • @user-bq2lb7kn2e
    @user-bq2lb7kn2e 5 лет назад +12

    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!

  • @filip8467
    @filip8467 3 года назад +43

    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)

  • @axeedo
    @axeedo 5 лет назад +6

    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!

  • @zombnie
    @zombnie 5 лет назад +11

    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

  • @AshishBurnwal
    @AshishBurnwal 6 лет назад +44

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

    • @habdochkeineahnung
      @habdochkeineahnung 4 года назад

      are you crazy? this was way too fast and he didnt explain what he did via autogenerate. thats not good teaching imho.

    • @mrp12-ii
      @mrp12-ii 3 года назад

      @@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.

  • @hasmi_intentart
    @hasmi_intentart 5 лет назад +15

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

  • @AnirudhSharma9211
    @AnirudhSharma9211 3 года назад +16

    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.

    • @praneethachebolu278
      @praneethachebolu278 Год назад

      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?

  • @maneaniket4200
    @maneaniket4200 5 лет назад +4

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

  • @alperozdamar517
    @alperozdamar517 4 года назад +1

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

  • @dnyaneshtathode5511
    @dnyaneshtathode5511 3 года назад +3

    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

  • @health.upgradedbyscience.7309
    @health.upgradedbyscience.7309 2 года назад +2

    Brilliant explanation, so important to know both approaches - i was not aware of the second one before - thanks a million!! 😆

  • @hughsoward3484
    @hughsoward3484 3 года назад +1

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

  • @deepakshakya9597
    @deepakshakya9597 3 года назад +2

    Your teaching skills are amazing!!! Thank you so much for making such valuable videos

  • @dgabrielm
    @dgabrielm 6 лет назад +3

    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!

  • @shivaramkrishnadurgi4253
    @shivaramkrishnadurgi4253 2 года назад +1

    Mr. Perfect @Navin Reddy... Loved your teaching style...Awesome!!!

  • @jatinderarora2261
    @jatinderarora2261 5 лет назад +2

    Awesome. Thanks for sharing your excellent knowledge !

  • @yatin1370
    @yatin1370 Год назад

    Hi Naveen, I thoroughly enjoyed the way you have put all the facts before us to understand this with crystal clarity!
    Thanks!

  • @purnimadas3762
    @purnimadas3762 6 лет назад +1

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

  • @vijayleo1430
    @vijayleo1430 5 лет назад

    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

  • @sajidmohammed8553
    @sajidmohammed8553 4 года назад

    Simple..crisp...to the point! and concepts made easy!

  • @nasamind
    @nasamind 5 лет назад +1

    Nice.. your explanation is very simple.. I went through other explanations but yours is easy to understand..

  • @programmingforfun4158
    @programmingforfun4158 2 года назад

    His explanation is so easy that you are bound to learn. If you can't learn from him, then coding is not for u

  • @learnshare9893
    @learnshare9893 2 года назад +9

    "Do simple things, simple things are great" - This Video

  • @ektagoyal3172
    @ektagoyal3172 2 года назад

    Best tutorial that made so complex logic seem so simple

  • @madhavilangute1706
    @madhavilangute1706 3 года назад

    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. 👍👍

  • @ctapiero
    @ctapiero 3 года назад +1

    You saved my midterm! Thank you sir!

  • @Matthew-McCallister
    @Matthew-McCallister 2 года назад

    Thank you so much! I was totally lost on how comparators worked until this video.

  • @rudrakshjhalani1681
    @rudrakshjhalani1681 5 лет назад +1

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

  • @deepthipakki6269
    @deepthipakki6269 4 года назад

    Comparator custInt = (a,b) -> a>b?1:-1; will work. If it is in the same line this works.

  • @MrMPRMP
    @MrMPRMP 2 года назад

    Excellent way to describe when to go for Comparable vs Comparator

  • @kbhardwaj1989
    @kbhardwaj1989 5 лет назад +1

    You explanation are always superb. Thanks you.

  • @RahulSharma-yr7mi
    @RahulSharma-yr7mi 2 года назад

    Mashallah, explained very well, hats off sir😊

  • @Pravin-hj2wj
    @Pravin-hj2wj 4 года назад

    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.

  • @mohammadhaque1873
    @mohammadhaque1873 4 года назад

    Excellent. Awesome and no one likely to have problem understanding. Thank You.

  • @sowmyal84
    @sowmyal84 4 года назад

    The perfect explanation.. you are the java encyclopaedia sir!!

  • @soldadopreciso
    @soldadopreciso 3 года назад

    Thanks pal , very useful video. Greetings from Colombia.

  • @Hufsa
    @Hufsa 2 года назад

    First time here and I totally understand why you have 1.57 M subs, you are a real master! awesome explanation.

  • @clashwithjerin6127
    @clashwithjerin6127 3 года назад

    Perfect.
    i was roaming and watching different videos about this, Finally found the perfect one !!

  • @dataguy7013
    @dataguy7013 3 года назад

    Very nice video on just the topics you are wondering about. Teluko, tussi great ho ji!

  • @sunnysaini4320
    @sunnysaini4320 6 лет назад +1

    sir great .....i m java developer.....your teach way is too good(tussi kmaal krde hunde aa)

  • @Evilshrimp02
    @Evilshrimp02 4 года назад +2

    This guy is the man. Thanks a lot.

  • @sagardarade
    @sagardarade 3 года назад

    Crystal clear in short time

  • @sheriffcrandy
    @sheriffcrandy 3 года назад +1

    Bro I love you man haha I was so confused on the differences between comparable and comparator.

  • @sankeths3508
    @sankeths3508 Год назад +1

    thank you sir, this video was crystal clear for me

  • @ravishinde4736
    @ravishinde4736 3 года назад

    Clear cut explanation, thank you sir

  • @subhakantaroul
    @subhakantaroul 3 года назад

    you are awesome! completely understood the concepts. before thins wherever I was reading about comparable and comparator, I was getting more confused.

  • @purnimadas3762
    @purnimadas3762 5 лет назад +1

    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!

  • @saikumarnarani8173
    @saikumarnarani8173 2 года назад

    Simply Amazing..!!! Thank you so much

  • @AhmedVlogs1
    @AhmedVlogs1 6 лет назад +4

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

  • @user-du6cx2zh4e
    @user-du6cx2zh4e 11 месяцев назад

    Thank you so much sir, this was the easiest explanation I could fine online.

  • @bharatmakkar9706
    @bharatmakkar9706 Год назад

    crystal clear explanation! thanks!

  • @nagarajanerode
    @nagarajanerode 3 года назад

    Great explanation in a simplest way possible.

  • @sudhanshusharma169
    @sudhanshusharma169 6 лет назад

    Ur way of teaching is very lucid and impressive too. Please make some videos on java 8 and 9.

  • @96Ballons
    @96Ballons 2 года назад

    Great explanation and examples!

  • @shubhamnigam14
    @shubhamnigam14 Год назад

    Easy simple and crisp ❤thanks

  • @lifeofsurya1261
    @lifeofsurya1261 Год назад

    Easily understood thanks bro

  • @IsaacAsante17
    @IsaacAsante17 5 лет назад +1

    Your tutorial is perfect. Thanks.

  • @amitchaudhary7745
    @amitchaudhary7745 2 года назад

    best explanation available on youtube

  • @rahulkhandelwal46578
    @rahulkhandelwal46578 3 года назад

    Perfect explanation.Thanks

  • @bharatmakkar9706
    @bharatmakkar9706 Год назад

    The explanation was amazing. Would be glad if you could deep dive a bit into the functioning of that compareTo and compare function.

  • @suryasikharej8116
    @suryasikharej8116 5 лет назад

    I have never seen a teacher like you sir....you are damn awesome

  • @balakrishnamaddula3695
    @balakrishnamaddula3695 3 года назад

    Thanks for good explanation sir !

  • @rajasekaran1724
    @rajasekaran1724 3 года назад

    Awesome Explanation

  • @adityasinghrajput4600
    @adityasinghrajput4600 4 года назад

    very clear and precise.. thanks a lot

  • @himanshukandwal3203
    @himanshukandwal3203 3 года назад

    Perfect bro..🙂🙂 you are good teacher.

  • @ArquimedesOfficial
    @ArquimedesOfficial 3 года назад +1

    Amazing tutorial man, thanks.

  • @ferfykins
    @ferfykins 2 года назад

    Excellent video, very well explained, ty so much!

  • @vadirajjahagirdar9342
    @vadirajjahagirdar9342 6 лет назад

    You are awesome... You teach in such a manner that anyone can pick

  • @kristiyangerasimov6708
    @kristiyangerasimov6708 2 года назад

    awesome video, thank you Telusko!

  • @jishuschannel
    @jishuschannel 4 года назад

    Very clean and clear way of explaining...no probable doubts you are leaving for your aliens...!!👍👍

  • @SuranjanaVlogsUSA
    @SuranjanaVlogsUSA 5 лет назад +1

    Such picturesque explanation 👌

  • @laxmichandra4904
    @laxmichandra4904 5 лет назад

    Sir your great .. you have given good teaching methodology.....thank you so much

  • @leosheng9311
    @leosheng9311 2 года назад

    Dude that's a perfect explanation, thank you.

  • @krishjlk
    @krishjlk Год назад

    For Final and Abstracr classes we can use Comparator becoz we cannot make them comparable .. Wonderful Explanation

  • @aathiranair704
    @aathiranair704 3 года назад

    So well explained!! Thank you so much ...

  • @VishalThakur-zg7ub
    @VishalThakur-zg7ub 3 года назад

    Yes, I enjo.. I mean understood what is comparable.

  • @CHAMPIONLTU
    @CHAMPIONLTU 4 года назад

    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! :)

  • @sachinchinnannavar9514
    @sachinchinnannavar9514 6 лет назад

    Naveen sir i was having lot of confusion with this concepts thanks for exlpaining it clearly with example

  • @karthick2237
    @karthick2237 5 лет назад +3

    Oh we have to mention explicitly, 👍

  • @bimalkumar712
    @bimalkumar712 3 года назад

    Cool...nice contents

  • @kiranmaiganapuram9079
    @kiranmaiganapuram9079 3 года назад

    wow very well explained sirr :) tq

  • @aartikelkar397
    @aartikelkar397 3 года назад

    Perfect explanation 👍

  • @greenxdshadow6635
    @greenxdshadow6635 Год назад

    bless you man, you make it so easy

  • @doniagasmi2192
    @doniagasmi2192 2 года назад

    great explanation! thank you

  • @kavitamohan6317
    @kavitamohan6317 3 года назад

    Excellent explanation.Thanks :) I am new subsscriber

  • @NourAli-hl4ih
    @NourAli-hl4ih 5 лет назад +1

    Underrated. After watching the whole internet you are the best hands down

    • @O-.-O
      @O-.-O 4 года назад

      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

  • @abrehamish
    @abrehamish 3 года назад

    Well explained

  • @manikandan2890
    @manikandan2890 3 года назад

    thank you sir my big question is clear

  • @jongmagee
    @jongmagee 5 лет назад

    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);
    }

  • @halamercy8594
    @halamercy8594 4 года назад

    You are far better than my professors,
    Thank you sir 🤗

  • @dipak002
    @dipak002 5 лет назад +1

    So well explained! Really appreciate... 👍

  • @shubhanksrivastava3590
    @shubhanksrivastava3590 6 лет назад

    explained it, in quite and easy manner... Thanks a lot.

  • @krishnakrishna-yh9ly
    @krishnakrishna-yh9ly 4 года назад

    Great

  • @pranavnyavanandi9710
    @pranavnyavanandi9710 Год назад

    Very nice, learnt a lot.

  • @Prashant-le3bq
    @Prashant-le3bq Год назад

    Nice explanation.

  • @Efocusto
    @Efocusto 3 года назад

    nice work big like

  • @andreme7326
    @andreme7326 3 года назад

    I enjoy your tutorials! Thank you, Navin!