import java.util.TreeSet;
public class TestTree {
public static void main(String[] args) {
new TestTree().go();
}
public void go() {
Book b1 = new Book("How cats Work");
Book b2 = new Book("Remiz your body");
Book b3 = new Book("Finding Emo");
TreeSet<Book> tree = new TreeSet<Book>();
tree.add(b1);
tree.add(b2);
tree.add(b3);
System.out.println(tree);
}
}
class Book implements Comparable{
String title;
public Book(String t) {
title = t;
}
@Override
public int compareTo(Object o) {
Book book = (Book) o;
return (title.compareTo(book.title));
}
}
Query
Getting output as : [programs.Book@5a10411, programs.Book@2ef1e4fa, programs.Book@306a30c7]
But need output : Finding Emo , How cats Work , Remiz your body
Expected output should be title of the books been sorted. But while printing tree object getting the >>object values :( Could some one help to sort out what mistake i have done? I am new to collections and learning them, need some help friends.