I went through too many resources that couldn't explain to me that all comparable does is return a positive, 0, or negative number based on the current instantiated object and the object passed in its parameters. They just go ahead and start using it. Thank you for understanding how to TEACH.
+John Jackson My pleasure... it frustrates me to no end when people don't explain the simple things sometimes... especially something as fundamental as the positive / 0 / negative thing with "compareTo"
A nice explanation for which am searching for a long time. Very good effort taken in letting others understand. Thank you so much, and it helped me a lot.
return "this" - but i have many objects of that class? which object is it comparing against??? And when I run collections(sort)(however its spelled) what is my input object that I compare with? :()
When you run Collections.sort(), it will compare each object IN that collection against each other object IN that same collection. For you to best understand what it means by "return this", just put a breakpoint in the compare code and you'll see how it iterates through each object in the collection and runs its compare. Give it a shot and if you're still unsure of what's going on, just post another comment and I'd be happy to help you out :)
Can you please explain to me what this.username is? I am not sure I got that part. user.getUsername is referring to the username being passed that is tpage or zpage, right? Because user.setUsername("tpage") or user2.setUsername("zpage") was what was passed, but what about this.username?
I have an assignment due and it asks me to create a subclass of the ArrayList which is obviously a superclass. I have started creating it but have become stuck on how to implement the compareTo method...I am not 100% sure if this SortedArrayList class (which has to sort out the elements in ascending order) should have been done within the user class, or on its own. If it is on its own, how would I go about setting the compareTo method?
Nice Video, However I have a doubt:1. Before sort the list has @list(0) = zpage and @list(1)= tpage.2. While compareTo method executing its returning the -ve value3. After execution we have list like @list(0) = tpage and @list(1) = zpage Doubt is if it returns the negative value it should not switch the objects, (as you told in video) however object are switching in list. Could you please explain this. I am trying to understand your statement on this.
Please,if you want to sort a list of citities, but the catch is you want to sort them in base to an atribute in they have called "longitude of the city"(it's and int), how would you make it so you can compare it by a determined atribute of an object?
+seiji fujii When you implement Comparable, you should pass in the object you wish to compare. This then allows you to compare based on properties of that object So for example: public class City implements Comparable { private double longitude; // getters and setters public int compareTo (City city) { return this.longitude.compareTo(city.longitude); } }
Great explanation. Could you please explain this "Comparable cannot sort objects on different attributes, while Comparator can" ? bcz I believe I have implemented comparable where I can sort based on different attributes. thanks
+anant rao All classes that are sub of java.lang all wrapper classes have a compare method already for example in double. return Double.valueOf(this.cost).compareTo(Double.valueOf(temp.cost));
Good tutorial. I have a doubt. For Comparator you have used compareToIgnoreCase method to customize sorting (Z and t). Isn't same can be used inside compareTo method by extending Comparable class to achieve customized sorting. i.e inside compareTo method also I can use compareToIgnoreCase to customize sorting. I didn't get the exact difference between Comparable and Comparator.
You can do that if you are dealing with customised class which is implementing Comparable interface. But for a list of strings, you have to use comparator inorder to define ur own sorting as String is a final class. Sting class cant be extended and it implements Comparable interface. So in its compareto(), the sorting method is already defined which doesnt consider "IgnoreCase" scenario as explained. Inorder to modify the sorting method to include "ignoreCase". we use Comparator.. Hope its clear now.. :)
thank u... i get to know what this and the user object actually refer to? also suggest me -- when i hover over this and user object in the statement return this.username.compareTo(user.getUsername()) in class users, my eclipse is not suggesting me which this object is referring to. please suggest me how do i get these suggestions from eclipse.
Amazingly good! Trevor, I have been following your tutorials and have one recommendation. Can you call out all the neat shortcuts you use in your video? I know you do talk about them in other videos, but if you could just call them out as you are working through the exercise that would be awesome. It's really beneficial for newbies.
Awesome explanation and communication skills...... That's called Experience. Thanks. and what to use in eclipse in the place of "assert(list.get(0).getUsername(), is("tpage"));" to create this program...??
+subodh agrawal compareToIgnoreCase will compare two strings and ignore the case of the strings. I believe the lower letter 'a' would be sorted before upper letter 'A'. So if your sorting mechanism is NOT case sensitive (i.e. you don't want to treat 'a' any different than 'A') then you would use compareToIgnoreCase()
+subodh agrawal The compareTo method for strings in JAVA uses lexicographical ordering which is a specific way to define how it decides the value of strings and can compare them by using this ordering. In the order they define upper case and lower case as two different entities much like ASCII. The ignore case simply doesn't define the difference between A or a. In other words instead of using compareToIgnoreCase() you could change the string to all uppercase or all lowercase and use compareTo(). But ofcourse if it's built into the language, don't do that.
import org.hamcrest.CoreMatchers.*; is not being recognised .. I have www.java2s.com/Code/Jar/h/Downloadhamcrestcore13jar.htm downloaded and in my plugins folder and the buildpath is seeing it .. but it wont recognise it . and junit is imported but cant see the junit window down below .. anyone know how to fix these things?
I think what I'm saying here is that in order to use Comparable, that would mean we need to add "implements Comparable" to the String class... but in Java we can't modify the String class at all, it's sort of "native" code. So the next best option is to use a Comparator.
java.lang.Error: Unresolved compilation problems: The method is(String) is undefined for the type test The method is(String) is undefined for the type test i am getting the error even after importing import the library as well
Springsource Tool Suite (spring.io/tools) - it's pretty much identical to Eclipse, but it has a few neat tools that integrate well with the Spring framework
Was very helpful, thank you! Just wish the video would've been a bit shorter. More straight to the point or a marker that skips the beginning stuff and goes straight to the point. Other than that very informational and helped me to understand a lot better!!!
very detailed explanation but inside the body of the compareTo() method You duidn't to write this.username.compareTo(user.getUsername(); you could of leave out the this keyword.
One of the worst videos by being not able to get to the straight point! Normal people would first explain everything and THEN make a review of possible mistakes!
This is the best explanation of these 2 interfaces I've seen so far. I'll recommend it to anyone I know struggling with the sorting concept
Much appreciated
You are a legend man! Definitely have some of the best tutorials on TH-cam, stay blessed!
all i wanted to know was in between 26:00 to 27:55(about comparator only) still watched full video...
you are an artist man!!! (LEVEL-VETERAN).
thanks
Great explanation. Everything is simple and clear. Much better than UNIVERSITY lessons i have been to. Keep up the good work!
"It goes back to being happy!" That's exactly how I feel about compilers too ahahahah
Useful video, was taking a course where this was discussed but was very vague, this helped fill in any of the gaps, thank you!
I went through too many resources that couldn't explain to me that all comparable does is return a positive, 0, or negative number based on the current instantiated object and the object passed in its parameters. They just go ahead and start using it. Thank you for understanding how to TEACH.
+John Jackson My pleasure... it frustrates me to no end when people don't explain the simple things sometimes... especially something as fundamental as the positive / 0 / negative thing with "compareTo"
A nice explanation for which am searching for a long time. Very good effort taken in letting others understand. Thank you so much, and it helped me a lot.
Great explanation thank you. Using the debug tool to emphasize what's actually happening during runtime helped a lot too.
Fantastic explanation, I've been trying to figure this out for a while and I instantly got it when I saw your video! Thank you!
Madalena Pavão boo yah!
A very nicely explained video. Helped me to understand the concept better.
superb .......finally after reading many blog and videos .I found this one as a best which clarify behind story of comparable interface
It is great to watch pre-lambda Comparator implementation
Best video for comparable and comparator interface ! Thanks for putting the concepts clear
Exceptional and brilliant teaching style..Thank you is just a word to show my gratitude to you sir..A big hug from India :)
I love this video! You explain really well. Please continue making videos.
Superb explanation. HATS OFF
Great Explanation. I appreciate the debugging details.
How does the Comparators's compare method know that veriables o1 and o2 are referring to the list's values in spot 0 and 1????
Nice example given to explain logic... Keep it up
You sir saved my exam!Thank you!
return "this" - but i have many objects of that class? which object is it comparing against??? And when I run collections(sort)(however its spelled) what is my input object that I compare with? :()
When you run Collections.sort(), it will compare each object IN that collection against each other object IN that same collection. For you to best understand what it means by "return this", just put a breakpoint in the compare code and you'll see how it iterates through each object in the collection and runs its compare.
Give it a shot and if you're still unsure of what's going on, just post another comment and I'd be happy to help you out :)
i don't understand English but I very understanded your tuto,thank you
Vous êtes le maître de JAVA
Man that was great, you are very good at explaining!
We can use compareToIgnoreCase in class that implements comparable. Can you explain any other usecase of comparator.
I love you! I finally know (the missing part) how to sort my LinkedList thank you!
Glad to be of service
Awesome explanation..
Really helpful! Awesome explanation
Beautifully explained
A perfect comparison.. what i actually wanted.. Thank you sir :-)
Very well explained..!!
Nice explanation and it is very helpful. Thank you for uploading these videos
Can you please explain to me what this.username is? I am not sure I got that part. user.getUsername is referring to the username being passed that is tpage or zpage, right? Because user.setUsername("tpage") or user2.setUsername("zpage") was what was passed, but what about this.username?
go to time 16:30 in the video you will get your answer
I have an assignment due and it asks me to create a subclass of the ArrayList which is obviously a superclass. I have started creating it but have become stuck on how to implement the compareTo method...I am not 100% sure if this SortedArrayList class (which has to sort out the elements in ascending order) should have been done within the user class, or on its own. If it is on its own, how would I go about setting the compareTo method?
This is really very helpful & good explanation of this. Thanks you so much.
Really clear. Thanks for sharing the knowledge!
Thank YOU for the feedback, glad the video was helpful
Which IDE are you using ?
I'm using the Spring Tool Suite, you can download it here: spring.io/tools
Thank You!
Nice Video, However I have a doubt:1. Before sort the list has @list(0) = zpage and @list(1)= tpage.2. While compareTo method executing its returning the -ve value3. After execution we have list like @list(0) = tpage and @list(1) = zpage Doubt is if it returns the negative value it should not switch the objects, (as you told in video) however object are switching in list. Could you please explain this. I am trying to understand your statement on this.
Thanks you now I am good :)
Thank you User(John bogdashkin) !!!!///@\\\!!!!
Could you please explain this even I have same doubt? How the objects are getting switched since it returns -ve value?
Please,if you want to sort a list of citities, but the catch is you want to sort them in base to an atribute in they have called "longitude of the city"(it's and int), how would you make it so you can compare it by a determined atribute of an object?
+seiji fujii When you implement Comparable, you should pass in the object you wish to compare. This then allows you to compare based on properties of that object
So for example:
public class City implements Comparable
{
private double longitude;
// getters and setters
public int compareTo (City city)
{
return this.longitude.compareTo(city.longitude);
}
}
thanks!
Great explanation. Could you please explain this "Comparable cannot sort objects on different attributes, while Comparator can" ? bcz I believe I have implemented comparable where I can sort based on different attributes. thanks
What if you are comparing float instead of string in compareTo method
+anant rao All classes that are sub of java.lang all wrapper classes have a compare method already for example in double.
return Double.valueOf(this.cost).compareTo(Double.valueOf(temp.cost));
very good explanation, make the video clear.
absolutely amazing video, exactly my style of learning. thank you very much
Good tutorial.
I have a doubt.
For Comparator you have used compareToIgnoreCase method to customize sorting (Z and t).
Isn't same can be used inside compareTo method by extending Comparable class to achieve customized sorting.
i.e inside compareTo method also I can use compareToIgnoreCase to customize sorting.
I didn't get the exact difference between Comparable and Comparator.
You can do that if you are dealing with customised class which is implementing Comparable interface. But for a list of strings, you have to use comparator inorder to define ur own sorting as String is a final class. Sting class cant be extended and it implements Comparable interface. So in its compareto(), the sorting method is already defined which doesnt consider "IgnoreCase" scenario as explained. Inorder to modify the sorting method to include "ignoreCase". we use Comparator..
Hope its clear now.. :)
brilliant, at last understood concept. Thank you.
nice explanation and it is very helpful. Thank you for uploading
You explained it really clearly thanks!
You're the man
thank u... i get to know what this and the user object actually refer to? also suggest me --
when i hover over this and user object in the statement return this.username.compareTo(user.getUsername()) in class users, my eclipse is not suggesting me which this object is referring to. please suggest me how do i get these suggestions from eclipse.
Super useful. Thank you so much
Everything makes sense now thanks !
Excellent Explanation.Thanks alot
how to sort based on both id,name
Amazingly good! Trevor, I have been following your tutorials and have one recommendation. Can you call out all the neat shortcuts you use in your video? I know you do talk about them in other videos, but if you could just call them out as you are working through the exercise that would be awesome. It's really beneficial for newbies.
Sure thing, I usually try to say the shortcut keys I'm using, but I guess I didn't stay true to that habit in this video. My bad!
Awesome explanation and communication skills...... That's called Experience. Thanks. and what to use in eclipse in the place of "assert(list.get(0).getUsername(), is("tpage"));" to create this program...??
OMFG, I sorted through dozens of HINDI videos to find you. Thanks.
I feel ya. Glad you found this video :)
that was great help in knowing the difference.. thanks :)
Thank you, I finally get it now.
But in comparable there is also compareToIgnoreCase() method. please tell me exact difference b/w these two.
+subodh agrawal compareToIgnoreCase will compare two strings and ignore the case of the strings. I believe the lower letter 'a' would be sorted before upper letter 'A'. So if your sorting mechanism is NOT case sensitive (i.e. you don't want to treat 'a' any different than 'A') then you would use compareToIgnoreCase()
+subodh agrawal The compareTo method for strings in JAVA uses lexicographical ordering which is a specific way to define how it decides the value of strings and can compare them by using this ordering. In the order they define upper case and lower case as two different entities much like ASCII. The ignore case simply doesn't define the difference between A or a.
In other words instead of using compareToIgnoreCase() you could change the string to all uppercase or all lowercase and use compareTo(). But ofcourse if it's built into the language, don't do that.
import org.hamcrest.CoreMatchers.*; is not being recognised .. I have www.java2s.com/Code/Jar/h/Downloadhamcrestcore13jar.htm downloaded and in my plugins folder and the buildpath is seeing it .. but it wont recognise it . and junit is imported but cant see the junit window down below .. anyone know how to fix these things?
Why ya said it is impossible to do compareToIgnoreCase() in @Override compareTo() when it is possible?
When did I say that in the video?
25:35 "...creating custom sorting method for Strings. But we can't do that with Comparable, can we? Well, not exactly."
I think what I'm saying here is that in order to use Comparable, that would mean we need to add "implements Comparable" to the String class... but in Java we can't modify the String class at all, it's sort of "native" code. So the next best option is to use a Comparator.
ahaaa, OK now it is clear why ya've chose a String to the second example, not an Object. OK :)
You got it :)
awesome explanation, thank you!!
java.lang.Error: Unresolved compilation problems:
The method is(String) is undefined for the type test
The method is(String) is undefined for the type test
i am getting the error even after importing import the library as well
What IDE is he using?
Springsource Tool Suite (spring.io/tools) - it's pretty much identical to Eclipse, but it has a few neat tools that integrate well with the Spring framework
I've been using net beans and this looks legit. It makes your set and get method for you!? That's awesome. That will save me some fat finger time. Lol
+Brandon Fischer netbeans does it to. Right click->refractor->Encapsulate fields-> Then click which you want
+Brandon Fischer Alt + insert lets you automatically create getters and setters too.
Great Explanation.. Thank you so much.
video is good, but please show us your full screen and not a zoomed in area. it is frustrating to follow the cursor around.
the number returned is the difference between the index of the two strings compared actually... z-6 would be t... z-2 would be x and so on...
Great video, kinda saved my life :D
Was very helpful, thank you! Just wish the video would've been a bit shorter. More straight to the point or a marker that skips the beginning stuff and goes straight to the point. Other than that very informational and helped me to understand a lot better!!!
Awesome Explanation!!!!!!!!! great!!!!!!!!
Very helpful.
Thx! Good explained!
Can't instantiate Comparator (abstract class), thats what netbeans says.
Thanks man! I finally understood it lol
Bit confusing can you please clear it , and run it with java do not run with junit test and take more properties so that we can easily understand.
Excellent!
indeed very helpful !
Thank you :)
Very helpful
thank you soooo much helped a lot
Thank you
Wow! Thank you. :)
Thank you very much very helpful
very detailed explanation but inside the body of the compareTo() method You duidn't to write this.username.compareTo(user.getUsername(); you could of leave out the this keyword.
Nice explanations but I am completely confused ......
Excellent. I listened to your podcast too. bum bum ba-dum ba-dum bum ba-bum bum ba-dum ba-dum bum
thaank you
Thanks
Yo this vid was soo gud
Excellent tutorial but uploading a code would help.
subscribed!
classy
Haha, glad you liked it!
👍🙌🙌
Thank you !!!!///@\\\!!!!
confusing
It is not confusing, Trevor did just great explanation,
I even learnt how to use JUnit Test and debug from this video :)
One of the worst videos by being not able to get to the straight point!
Normal people would first explain everything and THEN make a review of possible mistakes!
bad code! Never Users as a class name, put User. Never plural.
thanks for the video, it helped me very much.
indeed very helpful ! Thank you :)
Thank you