If we try to add user define class in TreeMap, the class must implement the Comparable interface else we will get ClassCastException.
Below program will throw ClassCastException as Book class does not implement the Comparable interface.
package adddingcutomobjectintreemap; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeMap; class Book { private String bookName; private int id; public Book(int id, String bookName) { super(); this.id = id; this.bookName = bookName; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public class Example { public static void main(String[] args) { Map<Book,String> treeMapObj = new TreeMap<>(); Book b1 = new Book(2,"alchemist"); Book b2 = new Book(3,"godan"); Book b3 = new Book(4,"gaban"); Book b4 = new Book(2,"mahabharta"); Book b5 = new Book(2,"ramayana"); treeMapObj.put(b1, "auther1"); treeMapObj.put(b2, "auther2"); treeMapObj.put(b3, "auther3"); treeMapObj.put(b4, "auther4"); treeMapObj.put(b5, "auther5"); Set<Map.Entry<Book, String>> set = treeMapObj.entrySet(); for(Map.Entry<Book, String> entry : set) { Book book = entry.getKey(); String auther = entry.getValue(); System.out.println("book id is "+book.getId() +" "+"book name is "+book.getBookName()); System.out.println("auther is " +auther); } } }
Output is –
Exception in thread “main” java.lang.ClassCastException: adddingcutomobjectintreemap.Book cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at adddingcutomobjectintreemap.Example.main(Example.java:47)
Let’s see an example where we are implementing the Comparable interface.
package adddingcutomobjectintreemap; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeMap; class Book implements Comparable<Book>{ private String bookName; private int id; public Book(int id, String bookName) { super(); this.id = id; this.bookName = bookName; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public int compareTo(Book book) { int bookId = book.getId(); return this.id-bookId; } } public class Example { public static void main(String[] args) { Map<Book,String> treeMapObj = new TreeMap<>(); Book b1 = new Book(2,"alchemist"); Book b2 = new Book(3,"godan"); Book b3 = new Book(4,"gaban"); Book b4 = new Book(2,"mahabharta"); Book b5 = new Book(2,"ramayana"); treeMapObj.put(b1, "auther1"); treeMapObj.put(b2, "auther2"); treeMapObj.put(b3, "auther3"); treeMapObj.put(b4, "auther4"); treeMapObj.put(b5, "auther5"); Set<Map.Entry<Book, String>> set = treeMapObj.entrySet(); for(Map.Entry<Book, String> entry : set) { Book book = entry.getKey(); String auther = entry.getValue(); System.out.println("book id is "+book.getId() +" "+"book name is "+book.getBookName()); System.out.println("auther is " +auther); } } }
Output is –
book id is 2 book name is alchemist
auther is auther5
book id is 3 book name is godan
auther is auther2
book id is 4 book name is gaban
auther is auther3