TreeSet的类强制转换异常问题

时间:2019-04-18 12:08:21

标签: java

好的,我有一个要解决的问题:

创建一个具有单个属性VirtualLibrary的泛型类totalNumberOfEntries,该类具有使用户能够设置和返回条目的方法。条目的类型为BookArticleMediaResourceMagazineManual。为每种类型的条目实现特定的类。 在main( )方法中,创建一个SortedSet<VirtualLibrary>变量,该变量将维护所有库条目。使用这些方法添加,添加多个,返回特定条目并检查库中是否存在条目。

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);

    }

}

我想不通。

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)

1 个答案:

答案 0 :(得分:3)

and对插入其中的数据进行排序。默认情况下,它不知道如何对Java类进行排序。

因此,您需要在构造函数中提供TreeSet,或者与Comparator一起使用的类应实现TreeSet

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
});