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

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

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

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

    New video - Comparable Interface vs Comparator Interface
    ruclips.net/video/kV05T7beMls/видео.html

  • @vxxxnni
    @vxxxnni 9 месяцев назад

    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 7 месяцев назад

    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!

  • @sallsu173
    @sallsu173 5 месяцев назад

    Nice explanation, keep the good work

  • @r_majka
    @r_majka 2 года назад +2

    Usefull and concise. Thank you:)

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

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

    Super easy to understand because of this video. 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")

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

    This helped out a ton, thank you!

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

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

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

    Thanks for the help, it was really useful!

  • @user-w87b7egvss
    @user-w87b7egvss Год назад

    Awesome video, easy to follow and understand :)

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

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

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

    It was very clear thanks lot 🙌

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

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

    Thanks man, this helped a lot!

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

  • @beanos1.071
    @beanos1.071 2 месяца назад

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

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

    How to compare string..?

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

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

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

  • @davidsanchez-bu9un
    @davidsanchez-bu9un 3 года назад

    Excellent!

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

    good vid :)

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

    thank u very much !

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

    Genius !

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

    perfection

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