Results 1 to 8 of 8
  1. #1
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default how to compare Strings with compareTo?

    I'm trying to understand and correctly implement the compareTo() method.
    So far I've seen that you need to use the class member you choose to order your class instances.
    Let's say I have a class with the members "name" (String) and "age" (integer), and I want to order the instances using the age as a comparison, I'd do something like this:

    public int compareTo(Object o)
    {
    if(!(o instanceof Student))
    throw new ClassCastException();
    if(((Student)o).age < this.getAge())
    return 1;
    if(((Alumno)o).age> this.getAge())
    return -1;
    else return 0;

    }


    But if I wanted to use the "name" member (which is a String), I can't use the < and > operators. As I've seen in my web searches, they use the name.compareTo()
    My question is: if I'm using compareTo() there, it's using it from the Object class, right?
    Is that correct? How does it work, if I'm using the compareTo() method inside my compareTo overridden method? It just sounds a bit wrong as a concept :D

    (my friend's answer was "just use it like that and stop nagging me", but I really want to know)

    Thanks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Recommended reading: String (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Thank you.
    My question was about the need to override the compareTo method (say, when you use a TreeSet). I didn't know why you would use compareTo when that's the method you're trying to replace.
    That's because the compareTo method in a specific class you made will not "know" how to compare its instances, but the String class has already a well defined behavior for it, right?

    Thanks again :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Pretty much, why re-invent the wheel when the smart people at sun/oracle already created a working version?

  5. #5
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Lol. That's right.
    I just heard so many times that "you HAVE to override compareTo() and equals()!" that it confused me. They should have added "but only for the classes you create" :p

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    haha yes, try to always use the built in compare to methods in your overridden compareTo method in your class. Also don't forget overriding hashCode().

  7. #7
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    Oh, does hashCode() have to be overridden too when using compareTo()? Because I read that hashCode() has to be consistent with equals(), using the same fields. Does it also have to be consistent with compareTo?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's good to override hashcode when overriding equals. It's good for hash based collections.

Similar Threads

  1. Compare two strings
    By roud9 in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 11:57 PM
  2. compare the remaining of Strings for equality
    By Cylab in forum New To Java
    Replies: 5
    Last Post: 08-19-2010, 02:42 PM
  3. Compare between 2 Strings
    By ChaosINC in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:39 AM
  4. [SOLVED] Cant compare strings sent by client
    By christuart in forum Networking
    Replies: 15
    Last Post: 09-01-2008, 03:24 AM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •