Comparable Interface in Java - Tutorial for Beginners | Learn Comparable in 5 minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ม.ค. 2025

ความคิดเห็น • 59

  • @javacodingcommunity
    @javacodingcommunity  ปีที่แล้ว

    New video - Comparable Interface vs Comparator Interface
    th-cam.com/video/kV05T7beMls/w-d-xo.html

  • @vxxxnni
    @vxxxnni 10 หลายเดือนก่อน

    Your explanation was simple and clear. What I was trying to figure out in 1 hour , you did in 5 minutes. Mind Blowing!

  • @JoshuaLawrence-wc2yr
    @JoshuaLawrence-wc2yr 8 หลายเดือนก่อน

    Busy practicing for a big practical test I have coming up and this video has been a lifesaver. Straight to the point, clear, and concise. Thank you so much for sharing!

  • @vanamutt43
    @vanamutt43 3 ปีที่แล้ว +4

    thanks mate, finally understand what this is.
    i watched several tutorials and my teacher talked about it and finally you made it clear why we need and how we can use it.

  • @draganjonceski2639
    @draganjonceski2639 ปีที่แล้ว +1

    Nice clear, concise and quick explanation!

  • @r_majka
    @r_majka 2 ปีที่แล้ว +2

    Usefull and concise. Thank you:)

  • @altayali7541
    @altayali7541 ปีที่แล้ว

    Super easy to understand because of this video. Thank you!

  • @sallsu173
    @sallsu173 5 หลายเดือนก่อน

    Nice explanation, keep the good work

  • @hans-henrikmadsen2003
    @hans-henrikmadsen2003 4 ปีที่แล้ว +3

    Hello, thank you for this video it is a great help for me. I do have one question : how happens if they have same id and you want the "compare method" to compare on something else instead of setting them equal to each other? like if they have same Id number, you want to compare the name then

    • @javacodingcommunity
      @javacodingcommunity  4 ปีที่แล้ว +3

      You can just add more if statements. If inside an if.

  • @CasualGamerJay
    @CasualGamerJay 3 ปีที่แล้ว +2

    This helped out a ton, thank you!

  • @javacodingcommunity
    @javacodingcommunity  4 ปีที่แล้ว +3

    Let me know if you need any help :)

    • @guydelacruz6042
      @guydelacruz6042 3 ปีที่แล้ว

      I need help using the comparable interface to display an Array list of objects on my GUI.

    • @love_death
      @love_death 2 ปีที่แล้ว

      how to sort alphabetically ?

    • @javacodingcommunity
      @javacodingcommunity  2 ปีที่แล้ว +1

      @@love_death In the video I sort by Id parameter but you can use name parameter aswell.
      Strings can also be compared with s1.compareTo(s2) whcih returns a number (0 if same, positive number if "larger" and negative number if "smaller")

  • @user-w87b7egvss
    @user-w87b7egvss 2 ปีที่แล้ว

    Awesome video, easy to follow and understand :)

  • @vicvancen
    @vicvancen 2 ปีที่แล้ว +1

    But how can I compare two Strings? I made an object name where the forenames and surnames are and want to sort them in main first by the surname and then by forename. Comparing two integers are rather simple but the strings... I dont understand that

    • @javacodingcommunity
      @javacodingcommunity  2 ปีที่แล้ว

      In your custom compareTo method you can use (www.javatpoint.com/java-string-compareto) and if the first result (comparing forenames) is equal then you do that again on surnames. If it is not equal just return what you got in first comparison.

  • @kinrev6719
    @kinrev6719 ปีที่แล้ว

    Thanks for the help, it was really useful!

  • @buraxta_
    @buraxta_ 2 ปีที่แล้ว

    It was very clear thanks lot 🙌

  • @sagarmoyganguly4140
    @sagarmoyganguly4140 2 ปีที่แล้ว

    Excellent video, tysm. But, what to do in case of maps / treemaps as sort function can only be used with lists and not with maps?

    • @javacodingcommunity
      @javacodingcommunity  2 ปีที่แล้ว

      TreeMap from docs: "A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. "
      Comparator: docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
      Regular map is not made for sorting but LinkedHashMap can be sorted for instance.

  • @otaldepeewee3596
    @otaldepeewee3596 ปีที่แล้ว

    Hi, I need ur help, when I use Array.sort it doesn´t work, can you teach me how to resolve ?

  • @TranTran-wc2mo
    @TranTran-wc2mo 3 ปีที่แล้ว

    Why when you change the return value for each if statement, the sort also change from largest to smallest?

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว

      If we return 1, that means that object upon which we called the method is larger, if i change the values that means that it is smaller, so everything changes.

  • @deli5777
    @deli5777 2 ปีที่แล้ว

    Thanks man, this helped a lot!

  • @noirRozelle
    @noirRozelle 3 ปีที่แล้ว

    I have a question, you didn't declare or used the overridden compareT() but it sorted it regardless, how did that happen?

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว +1

      I used Collections.sort(arr) which can be used on the car class because it implements the Comparable interface. So you don't use the compareTo() but the Collections.sort does.

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว

      Hope this helps :)

    • @noirRozelle
      @noirRozelle 3 ปีที่แล้ว

      @@javacodingcommunity Where can I see the actual code that compareTo() is used by the Collections.sort? I tried opening the Collections.sort implementation in IntelliJ but I did not see the compareTo() being used.

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว +1

      @@noirRozelle docs.oracle.com/javase/7/docs/api/java/util/Collections.html
      "public static

    • @noirRozelle
      @noirRozelle 3 ปีที่แล้ว

      @@javacodingcommunity ow I see. The sort method of collections extends to the comparable class thus when you overridden the compareTo() it is for the Collections.sort. Thank you for the explanation! This really confused me at first because of the versatility of compareTo() . Thank you again for the clear explanation :)

  • @beanos1.071
    @beanos1.071 2 หลายเดือนก่อน

    Why did you only write the get id method and not the get name?

  • @davidsanchez-bu9un
    @davidsanchez-bu9un 3 ปีที่แล้ว

    Excellent!

  • @thetruth5672
    @thetruth5672 2 ปีที่แล้ว

    Great. Awesome. So the Collections.sort(car) method will go and look inside compareTo() method, and see that which values are being compared. here we have id compared and the value 0,1,-1 is returned. and based upon these returned values the collections.sort(car) methods works.

  • @gringotico1759
    @gringotico1759 2 ปีที่แล้ว

    Alternatively for line 51, you can call the method arr.sort(null) and you'll accomplish the same.

  • @williamsenior717
    @williamsenior717 2 ปีที่แล้ว

    Genius !

  • @goldenraisins
    @goldenraisins 3 ปีที่แล้ว

    What is comparable actually for?

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว

      Java Comparable interface is used to order the objects of the user-defined class.

  • @SHUB74341
    @SHUB74341 ปีที่แล้ว

    How to compare string..?

  • @pollekiller3678
    @pollekiller3678 ปีที่แล้ว

    good vid :)

  • @vinodmanikanta
    @vinodmanikanta 3 ปีที่แล้ว

    when will compareTo() called ,

    • @javacodingcommunity
      @javacodingcommunity  3 ปีที่แล้ว

      I used Collections.sort(arr) which can be used on the car class because it implements the Comparable interface. So you don't use the compareTo() but the Collections.sort does.

    • @dharmalingeswarparasa1609
      @dharmalingeswarparasa1609 2 ปีที่แล้ว +1

      By implementing comparable, we are making sure that the object elements have the method compareTo() defined. Upon only which the collections.sort() method is able to do it's work ( it uses compareTo() method.) We don't use it directly, it's just a pre requisite for sort method.

  • @idfc7642
    @idfc7642 2 ปีที่แล้ว

    perfection

  • @davidbister2684
    @davidbister2684 3 ปีที่แล้ว

    thank u very much !

  • @akna1987
    @akna1987 ปีที่แล้ว

    good video but you forgot to add the equals() method

  • @zstar8397
    @zstar8397 ปีที่แล้ว

    hey i just wanna say that GOD loved the world so much he sent his only begotten
    son Jesus to die a brutal death for us so that we can have eternal life
    and we can all accept this amazing gift this by simply believing in him (Jesus) asking for the forgiveness of your sins
    and forming a relationship with heavenly father.