Sebi Cioată
Sebi Cioată

Reputation: 75

Class Cast Exception problems with TreeSet

Ok, I have this problem to solve:

Create a generic class called VirtualLibrary with a single attribute, totalNumberOfEntries, and with methods which enables the user to set and return an entry. The types of entries are Book, Article, MediaResource, Magazine and Manual. Implement the specific classes for each type of entry. In the main( ) method create a SortedSet<VirtualLibrary> variable which maintains all the library entries. Use the methods to add, add multiple, return a specific entry and check if an entry exists in the library.

import java.util.Collections;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;

class VirtualLibrary<T>{
    private int totalNumberOfEntries;
    void setTotalNumberOfEntries(int totalNumberOfEntries) {
        this.totalNumberOfEntries=totalNumberOfEntries;
    }
    int getTotalNumberOfEntries() {
        return this.totalNumberOfEntries;
    }
}
class Base{}
class Book extends Base{
}
class Article extends Base{ 
}
class MediaResource extends Base{
}
class Magazine extends Base{
}
class Manual extends Base{
}

public class P5 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        VirtualLibrary<Book> book = new VirtualLibrary<>();
        SortedSet<VirtualLibrary> lib = new TreeSet<>();
        int totalNumberOfEntries;
        System.out.println("Please insert the total number of entries of the book: ");
        totalNumberOfEntries=keyboard.nextInt();
        book.setTotalNumberOfEntries(totalNumberOfEntries);
        lib.add(book);
        VirtualLibrary<Article> article = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the article: ");
        totalNumberOfEntries=keyboard.nextInt();
        article.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<MediaResource> mediaResource = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the media resource: ");
        totalNumberOfEntries=keyboard.nextInt();
        mediaResource.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<Magazine> magazine = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the magazine: ");
        totalNumberOfEntries=keyboard.nextInt();
        magazine.setTotalNumberOfEntries(totalNumberOfEntries);
        VirtualLibrary<Manual> manual = new VirtualLibrary<>();
        System.out.println("Please insert the total number of entries of the manual: ");
        totalNumberOfEntries=keyboard.nextInt();
        manual.setTotalNumberOfEntries(totalNumberOfEntries);
        Collections.addAll(lib, article,mediaResource,magazine,manual);
        System.out.println(lib.first());
        System.out.println(lib.last());
        System.out.println(lib);

    }

}

I cant figure it out.

Exception in thread "main" java.lang.ClassCastException: class VirtualLibrary cannot be cast to class java.lang.Comparable (VirtualLibrary is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
    at java.base/java.util.TreeMap.compare(TreeMap.java:1291)
    at java.base/java.util.TreeMap.put(TreeMap.java:536)
    at java.base/java.util.TreeSet.add(TreeSet.java:255)
    at P5.main(P5.java:50)

Upvotes: 3

Views: 4288

Answers (1)

Hemant Patel
Hemant Patel

Reputation: 3260

TreeSet sorts data inserted in it. By default it doesn't know how to sort java classes.

So, either you need to provide a Comparator in constructor or the class you are using with TreeSet should implement Comparable.

// using comparator
SortedSet<ClassA> set = new TreeSet<>( new Comparator() {
    @Override
    public int compare(ClassA a, ClassA b) {
        // your implementation 
    }
});

// java 8 lambda
SortedSet<ClassA> set = new TreeSet<>( (a, b) -> {
   // your implementation
});

Or

class ClassA implements Comparable<ClassA> {

    ....
    @Override
    public int compareTo(ClassA other) {
        // your implementation
    }
}

Upvotes: 8

Related Questions