Interview Question | Comparable vs Comparator in Java

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

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

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

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

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

    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 4 года назад +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 4 года назад +5

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

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

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

  • @axeedo
    @axeedo 6 лет назад +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!

  • @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!

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

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

    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 2 года назад

      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?

  • @AshishBurnwal
    @AshishBurnwal 7 лет назад +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 4 года назад

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

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

    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)

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

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

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

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

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

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

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

  • @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!! 😆

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

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

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

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

  • @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!

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

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

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

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

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

    This guy is the man. Thanks a lot.

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

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

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

    You saved my midterm! Thank you sir!

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

    Awesome. Thanks for sharing your excellent knowledge !

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

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

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

    Cool...nice contents

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

    Great

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

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

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

    nice work big like

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

    Well explained

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

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

    crystal clear explanation! thanks!

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

    Crystal clear in short time

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

    Great explanation and examples!

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

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

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

    Your tutorial is perfect. Thanks.

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

    Mashallah, explained very well, hats off sir😊

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

    best explanation available on youtube

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

    Awesome Explanation

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

    Perfect explanation.Thanks

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

    Great explanation in a simplest way possible.

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

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

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

    Perfect explanation 👍

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

    You explanation are always superb. Thanks you.

  • @learnshare9893
    @learnshare9893 3 года назад +10

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

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

    Amazing video

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

    Great

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

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

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

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

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

    Outstanding!

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

    Such picturesque explanation 👌

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

    Best tutorial that made so complex logic seem so simple

  • @Shiva-zy7jq
    @Shiva-zy7jq 4 года назад

    Best explanation

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

    Easily understood thanks bro

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

    Amazing explanation

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

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

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

    Great job, thanks.

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

    Easy simple and crisp ❤thanks

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

    awesome video, thank you Telusko!

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

    Superb explanation bro..... Crystal clear

  • @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!

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

    Thanks for the video!

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

    guru ho bhai sahi me tum ,,, thanks much

  • @Prashant-le3bq
    @Prashant-le3bq 2 года назад

    Nice explanation.

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

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

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

    SO GOOD Thank you!!

  • @codetech-01
    @codetech-01 3 года назад

    Awesome!!

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

    Nicely explained!

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

    Wow! Just wow!

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

    The best explanation.. Thanks a lot

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

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

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

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

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

    very clear and precise.. thanks a lot

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

    bless you man, you make it so easy

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

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

    Great. 👍

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

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

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

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

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

    Excellent way to describe when to go for Comparable vs Comparator

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

    Amazing man, the best explanation by far

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

    great explanation! thank you

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

    Excellent

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

    Excellent video Telusko, I've now subbed to your channel!!

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

    Thanks pal , very useful video. Greetings from Colombia.

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

    Awesome

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

    So well explained! Really appreciate... 👍

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

    Thanks for good explanation sir !

  • @kavansingh-k3q
    @kavansingh-k3q Год назад

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

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

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

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

    Simply Amazing..!!! Thank you so much

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

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

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

    Clear cut explanation, thank you sir

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

    I enjoy your tutorials! Thank you, Navin!

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

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

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

    Perfect bro..🙂🙂 you are good teacher.

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

    Nice information.Thanks a lot ....

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

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

    Nice explanation.... Thank you ....

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

    Dude that's a perfect explanation, thank you.

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

    nice explanation

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

    You made it easy Sir. Thank you