Results 1 to 3 of 3
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default ArrayLists compareTo method, equals method

    Hi, I'm having a little bit of trouble with my compareTo and equals methods. I have implemented Comparable<Book>. The assignment is to Create a library of books and with each book has a title and an ArrayList of authors. The compareTo method should base the comparison on the title field. This is what I have so far:

    Java Code:
    public int compareTo(Book other) {
    	   return myTitle.compareTo(other.myTitle) ;
       }
    The equals method should compare Books base on their title and authors. This is what I have so far:

    Java Code:
    public boolean equals(Object o) {
    	   boolean flag = false ;
    	   Book book = (Book) o ;
    	   if (book.myTitle == myTitle) {
    		   flag = true ;
    	   }
    	return flag ;
    	   
    	   }
    I have not implemented the authors in the equals method, have not gone that far with it.

    I sort of understand what the compareTo method and equals method is doing, but not really at the same time. If anybody can shine some light on this. Thank You.

  2. #2
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    Having a class implement Comparable means that instance of that class can have some sort of order. In your case, your Book objects are ordered alphabetically based on their title. Read the API for Comparable for more information on how that works.

    Overriding equals imposes an equality on two different objects. That is in your case, a Book is equivalent to another Book if the title and the list of authors is the same.

    For example:

    Java Code:
    Book book1 = new Book(); // title = "Book Title 1"; author = "John Smith";
    Book book2 = new Book(); // title = "Book Title 2"; author = "Jane Doe";
    Book book3 = new Book(); // title = "Book Title 1"; author = "John Smith";
    
    book1 == book1 // true
    book1 == book2 // false
    book1.equals(book2) // false
    book1.equals(book3) // true
    book1 == book3 // false
    Last edited by literallyjer; 10-26-2009 at 08:27 PM.

  3. #3
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Ah okay gotcha alright Imma look up the api and check that out. I'll post more questions if I have any.

Similar Threads

  1. Replies: 5
    Last Post: 10-13-2009, 02:35 PM
  2. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  3. Replies: 8
    Last Post: 03-19-2009, 10:38 PM
  4. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 08:40 PM

Posting Permissions

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