My Treeset only adds 1 class object

允我心安 提交于 2020-01-30 11:52:06

问题


I'm trying to add the below book objects to a TreeSet. However, when I debug the code, it says that the set has a size of 1 and only contains the first object added (book1). When I comment out book1, book2 is the only one added etc.
Why is it that the JVm only recognises one object?

Code:

 public static void main(String[] args) {
    Set<Book> bookSet = new TreeSet<Book>();

    Book book1 = new Book("Digital Fortress", "Dan Brown", "St. Martins Press", 1998);
    Book book2 = new Book("Angels and Demons", "Dan Brown", "Pocket Books", 2000);
    Book book3 = new Book("Deception Point", "Dan Brown", "Pocket Books", 2001);
    Book book4 = new Book("The Davinci Code", "Dan Brown", "DoubleDay", 2003);
    Book book5 = new Book("The Lost Symbol", "Dan Brown", "DoubleDay", 2009);
    Book book6 = new Book("Inferno", "Dan Brown", "DoubleDay", 2013);

    bookSet.add(book1);
    bookSet.add(book2);
    bookSet.add(book3);
    bookSet.add(book4);
    bookSet.add(book5);
    bookSet.add(book6);

    System.out.println(bookSet);
}

Here is the entire code so far (It has to be all in the same class):

  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
  import java.util.TreeSet;

public class Book implements Comparable<Book> {

String title;
String author;
String publisher;
int publicationYear;

List<String> authorList = new ArrayList<String>();

public Book(String title, String author, String publisher, int publicationYear){
    this.title = title;
    this.author = author;
    this.publisher = publisher;
    this.publicationYear = publicationYear;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public List<String> getAuthorList() {
    return authorList;
}

public void setAuthorList(List<String> authorList) {
    this.authorList = authorList;
}

public String getPublisher() {
    return publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public int getPublicationYear() {
    return publicationYear;
}

public void setPublicationYear(int publicationYear) {
    this.publicationYear = publicationYear;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}


public String toString(){   
    return "Book Title: " + getTitle() + "\nAuthor Name: " + getAuthor() + "\nPublisher: " + getPublisher() + "\nYear of Publication: " + getPublicationYear();  
}

public static void main(String[] args) {
    Set<Book> bookSet = new TreeSet<Book>();

    Book book1 = new Book("Digital Fortress", "Dan Brown", "St. Martins Press", 1998);
    Book book2 = new Book("Angels and Demons", "Dan Brown", "Pocket Books", 2000);
    Book book3 = new Book("Deception Point", "Dan Brown", "Pocket Books", 2001);
    Book book4 = new Book("The Davinci Code", "Dan Brown", "DoubleDay", 2003);
    Book book5 = new Book("The Lost Symbol", "Dan Brown", "DoubleDay", 2009);
    Book book6 = new Book("Inferno", "Dan Brown", "DoubleDay", 2013);

    bookSet.add(book1);
    bookSet.add(book2);
    bookSet.add(book3);
    bookSet.add(book4);
    bookSet.add(book5);
    bookSet.add(book6);

    System.out.println(bookSet);
}

@Override
public int compareTo(Book o) {
    // TODO Auto-generated method stub
    return 0;
}  

}


回答1:


All books are equal to one another (evident by returning 0). A Set cannot contain duplicates, so that's why only one (the first Book) is ever added

@Override
public int compareTo(Book o) {
    // TODO Auto-generated method stub
    return 0;
}  

You need to fill in the details of the method. Negative numbers say this book is less than o, vice versa for positive numbers




回答2:


@Override
public int compareTo(Book o) {
    // TODO Auto-generated method stub
    return 0;
}

What Comparable#compareTo return value says -

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. documentation

When you added multiple object to tree set, a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.All the object are equals by Book objects compareTo method(provided) definition, so TreeSet just ignored duplicate object.

Solution: Define compareTo method correctly, keeping this thing in mind return value should negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.




回答3:


Update your compareTo method based on your requirement. Or at least update as below, it will allow to insert all objects in treeset.

public int compareTo(Book o) {
// TODO Auto-generated method stub
return this.compareTo(o);
} 


来源:https://stackoverflow.com/questions/40383909/my-treeset-only-adds-1-class-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!