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
  • Наука та технологія

КОМЕНТАРІ • 364

  • @saby8765
    @saby8765 6 років тому +211

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

  • @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 4 роки тому +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

  • @steviet5246
    @steviet5246 5 років тому +20

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

  • @hasmi_intentart
    @hasmi_intentart 5 років тому +15

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

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

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

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

  • @AshishBurnwal
    @AshishBurnwal 6 років тому +44

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

    • @habdochkeineahnung
      @habdochkeineahnung 3 роки тому

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

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

  • @rudrakshjhalani1681
    @rudrakshjhalani1681 5 років тому +1

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

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

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

  • @jatinderarora2261
    @jatinderarora2261 5 років тому +2

    Awesome. Thanks for sharing your excellent knowledge !

  • @AhmedVlogs1
    @AhmedVlogs1 6 років тому +4

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

  • @alperozdamar517
    @alperozdamar517 4 роки тому +1

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

  • @IsaacAsante17
    @IsaacAsante17 5 років тому +1

    Your tutorial is perfect. Thanks.

  • @hughsoward3484
    @hughsoward3484 3 роки тому +1

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

  • @gongjiaji2489
    @gongjiaji2489 5 років тому +1

    Thank you, clear and beautiful explanation.

  • @health.upgradedbyscience.7309
    @health.upgradedbyscience.7309 Рік тому +2

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

  • @deepakshakya9597
    @deepakshakya9597 2 роки тому +2

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

  • @yatin1370
    @yatin1370 Рік тому

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

  • @andreme7326
    @andreme7326 3 роки тому

    I enjoy your tutorials! Thank you, Navin!

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

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

  • @maneaniket4200
    @maneaniket4200 5 років тому +4

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

  • @obsydian806
    @obsydian806 4 роки тому

    This was such a helpful video. Thank you so much for making it.

  • @physicswithneerajbhaiya
    @physicswithneerajbhaiya 5 років тому

    Great way of explaining, understood in one go

  • @sajidmohammed8553
    @sajidmohammed8553 4 роки тому

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

  • @mohammadhaque1873
    @mohammadhaque1873 4 роки тому

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

  • @nasamind
    @nasamind 5 років тому +1

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

  • @bhanuprasad4952
    @bhanuprasad4952 5 років тому

    Clearly explained. Thanks a lot

  • @ThatGuyAnonymous
    @ThatGuyAnonymous 5 років тому

    Wow, plain and clear, thanks Sir 👍

  • @dipak002
    @dipak002 5 років тому +1

    So well explained! Really appreciate... 👍

  • @rameshp4604
    @rameshp4604 5 років тому

    Thank you, I got clear picture about the comparable and comparator

  • @mohammadmoslemuddin7274
    @mohammadmoslemuddin7274 4 роки тому

    Mind-blowing explanation. Thank you

  • @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 5 років тому +1

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

  • @SuranjanaVlogsUSA
    @SuranjanaVlogsUSA 5 років тому +1

    Such picturesque explanation 👌

  • @Jermiah-tj9qe
    @Jermiah-tj9qe 5 років тому

    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!

  • @sankeths3508
    @sankeths3508 Рік тому +1

    thank you sir, this video was crystal clear for me

  • @Sushil2874
    @Sushil2874 4 роки тому

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

  • @sachinchinnannavar9514
    @sachinchinnannavar9514 6 років тому

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

  • @shubhanksrivastava3590
    @shubhanksrivastava3590 6 років тому

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

  • @fRosT_verse
    @fRosT_verse 6 років тому +1

    damn good explanation... Much needed thanks sir :)

  • @Evilshrimp02
    @Evilshrimp02 4 роки тому +2

    This guy is the man. Thanks a lot.

  • @kbhardwaj1989
    @kbhardwaj1989 5 років тому +1

    You explanation are always superb. Thanks you.

  • @maayoufamoez2217
    @maayoufamoez2217 5 років тому

    Excellent, Well done!

  • @suryasikharej8116
    @suryasikharej8116 5 років тому

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

  • @randyafriansyah9340
    @randyafriansyah9340 4 роки тому

    thank you, I love your explanation

  • @jaaeamor7901
    @jaaeamor7901 Рік тому

    Thanks for the video!

  • @dataguy7013
    @dataguy7013 3 роки тому

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

  • @clashwithjerin6127
    @clashwithjerin6127 3 роки тому

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

  • @josephwong2832
    @josephwong2832 4 роки тому

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

  • @ajaysimon3064
    @ajaysimon3064 2 роки тому

    What a great video. Understood the concept clearly

  • @vladanulardzic5859
    @vladanulardzic5859 4 роки тому

    Great explanation ! Keep on !

  • @ezhilarasikrishnan5408
    @ezhilarasikrishnan5408 5 років тому

    It's amazing... Great explanation..

  • @sreekanthreddy5942
    @sreekanthreddy5942 6 років тому

    Nice information.Thanks a lot ....

  • @shivaramkrishnadurgi4253
    @shivaramkrishnadurgi4253 2 роки тому +1

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

  • @kchemutai3483
    @kchemutai3483 Рік тому

    Thank you so much, its very clear and precise

  • @bharatmakkar9706
    @bharatmakkar9706 Рік тому

    crystal clear explanation! thanks!

  • @rumajreij5437
    @rumajreij5437 6 років тому +1

    I love your explanations!

  • @chilinouillesdepommesdeter819
    @chilinouillesdepommesdeter819 6 років тому

    Consice and informative,great video

  • @jishuschannel
    @jishuschannel 4 роки тому

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

  • @vadirajjahagirdar9342
    @vadirajjahagirdar9342 6 років тому

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

  • @user-du6cx2zh4e
    @user-du6cx2zh4e 10 місяців тому

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

  • @jomagaray5563
    @jomagaray5563 4 роки тому

    thanks for the video !! greetings from Argentina !!

  • @kavya5808
    @kavya5808 5 років тому

    How can someone teach so well. You are awesome.

  • @Matthew-McCallister
    @Matthew-McCallister 2 роки тому

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

  • @greenxdshadow6635
    @greenxdshadow6635 Рік тому

    bless you man, you make it so easy

  • @TheChuster
    @TheChuster 5 років тому

    Clean, efficient explanation. thanks

  • @ctapiero
    @ctapiero 3 роки тому +1

    You saved my midterm! Thank you sir!

  • @DavidaThatch
    @DavidaThatch Рік тому

    SO GOOD Thank you!!

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

  • @siteshp
    @siteshp 5 років тому +1

    Nice Explanation...

  • @ngneer5804
    @ngneer5804 4 роки тому

    Very well explained, thank you.

  • @mahajanram4311
    @mahajanram4311 4 роки тому

    You are awesome!!! Thank you so much!!!

  • @ferfykins
    @ferfykins 2 роки тому

    Excellent video, very well explained, ty so much!

  • @saikumarnarani8173
    @saikumarnarani8173 2 роки тому

    Simply Amazing..!!! Thank you so much

  • @laxmichandra4904
    @laxmichandra4904 5 років тому

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

  • @kuldeepsahoo5080
    @kuldeepsahoo5080 5 років тому

    thanks for explaining in such a nice way

  • @MrPrakash57
    @MrPrakash57 6 років тому

    Thanks a lot navin. This is really helpful....

  • @leosheng9311
    @leosheng9311 2 роки тому

    Dude that's a perfect explanation, thank you.

  • @alexjohnlovesu
    @alexjohnlovesu 6 років тому

    Thanks, well explained

  • @rajenderprasad1193
    @rajenderprasad1193 Рік тому

    You made it easy Sir. Thank you

  • @vision2k890
    @vision2k890 4 роки тому

    Best explanation I've found, thanks

  • @anonimus4331
    @anonimus4331 4 роки тому

    Thank you. Very informative video

  • @doniagasmi2192
    @doniagasmi2192 2 роки тому

    great explanation! thank you

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

  • @akiratoriyama1320
    @akiratoriyama1320 4 роки тому

    Great explanation!! Thank you sir!!

  • @96Ballons
    @96Ballons 2 роки тому

    Great explanation and examples!

  • @MrMPRMP
    @MrMPRMP 2 роки тому

    Excellent way to describe when to go for Comparable vs Comparator

  • @ArquimedesOfficial
    @ArquimedesOfficial 3 роки тому +1

    Amazing tutorial man, thanks.

  • @kristiyangerasimov6708
    @kristiyangerasimov6708 2 роки тому

    awesome video, thank you Telusko!

  • @nagarajanerode
    @nagarajanerode 3 роки тому

    Great explanation in a simplest way possible.

  • @shubhamnigam14
    @shubhamnigam14 Рік тому

    Easy simple and crisp ❤thanks

  • @Ahsu0108
    @Ahsu0108 3 роки тому

    Thanks so much for the great explanation.

  • @sheriffcrandy
    @sheriffcrandy 3 роки тому +1

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

  • @sureshgarine
    @sureshgarine 5 років тому

    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.