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!
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.
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
@@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")
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
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.
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.
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 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 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 :)
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.
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.
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.
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.
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.
New video - Comparable Interface vs Comparator Interface
ruclips.net/video/kV05T7beMls/видео.html
Your explanation was simple and clear. What I was trying to figure out in 1 hour , you did in 5 minutes. Mind Blowing!
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!
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.
glad I can help :)
Nice clear, concise and quick explanation!
Nice explanation, keep the good work
Usefull and concise. Thank you:)
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
You can just add more if statements. If inside an if.
Super easy to understand because of this video. Thank you!
Let me know if you need any help :)
I need help using the comparable interface to display an Array list of objects on my GUI.
how to sort alphabetically ?
@@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")
This helped out a ton, thank you!
Glad I could help
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
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.
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?
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.
Hi, I need ur help, when I use Array.sort it doesn´t work, can you teach me how to resolve ?
It should be Arrays.sort
@@javacodingcommunity yes, but doesnt work
Thanks for the help, it was really useful!
Glad I could help!
Awesome video, easy to follow and understand :)
I have a question, you didn't declare or used the overridden compareT() but it sorted it regardless, how did that happen?
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.
Hope this helps :)
@@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.
@@noirRozelle docs.oracle.com/javase/7/docs/api/java/util/Collections.html
"public static
@@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 :)
Why when you change the return value for each if statement, the sort also change from largest to smallest?
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.
It was very clear thanks lot 🙌
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.
Yes
Alternatively for line 51, you can call the method arr.sort(null) and you'll accomplish the same.
Thanks man, this helped a lot!
I’m glad I could help!
What is comparable actually for?
Java Comparable interface is used to order the objects of the user-defined class.
Why did you only write the get id method and not the get name?
How to compare string..?
s1.compareTo(s2)
when will compareTo() called ,
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.
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.
good video but you forgot to add the equals() method
Excellent!
Thanks!
good vid :)
thank u very much !
No problem!
Genius !
perfection
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.