无法使用对象集合将哈希集转换为树集

can't convert hashset to treeset with object collection

我必须将 Hashset 转换为 TreeSet,但我有错误

// Here, All is ok, Chien is an object.
Set<Chien> animaux = new HashSet<Chien>();
animaux.add(new Chien("non", 10));
animaux.add(new Chien("zoz", 15));
animaux.add(new Chien("non", 10));

// And then i have to convert it to TreeSet 
// IntellJ-Idead says me that's ok...
// But i have an error here
TreeSet<Chien> treseet = new TreeSet<Chien>(animaux);

但是当我尝试编译时:

Exception in thread "main" java.lang.ClassCastException: fr.univtln.arouani277.vertebre.animal.Chien cannot be cast to java.lang.Comparable
    at java.util.TreeMap.put(TreeMap.java:559)
    at java.util.TreeSet.add(TreeSet.java:255)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:322)
    at java.util.TreeSet.addAll(TreeSet.java:312)
    at java.util.TreeSet.<init>(TreeSet.java:160)
    at fr.univtln.arouani277.App.main(App.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:622)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

这是我的文件 Chien :

public class Chien extends Mammifere implements Ibruit {
    public Chien(String pnom, int page) {
        super(pnom, page);
    }

    public void aboyer() {
        System.out.println("ouaf");
    }

    public void crier() {
        System.out.println("ouaf");
    }
}

它扩展了 Mammifere :

public abstract class Mammifere extends Animal {
    public Mammifere(String pnom, int page) {
        super(pnom, page);
    }
}

扩展 Animal :

public abstract class Animal extends Vertebre {
    public Animal(String nom, int page) {
        super(nom, page);
    }

    public void aboyer() {
    }

    public String toString() {
        System.out.println("Je suis un animal");
        return super.toString();
    }
}

扩展 Vertebrehttp://pastebin.com/Aa4Rw8sW

报错告诉你哪里出了问题:

ClassCastException: fr.univtln.arouani277.vertebre.animal.Chien cannot be cast to java.lang.Comparable

你应该让你的 class Chien 实现 Comparable. This is documented in the constructor of TreeSet(强调我的):

Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.

这是预期的,因为 TreeSet 对其元素进行排序,因此它必须知道如何比较它们。

由于您正在构建一个复杂的 class 层次结构,也许 Comparable 接口应该由顶层 class Vertebre 实现。这取决于您实际要如何进行比较。

HashSet 不同,TreeSet 依赖于排序,因此它可以快速确定是否存在重复项。当您将 HashSet 传递给 TreeSet constructor 时,它会发现您的 Chien 对象不是 Comparable.

Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.

您有 2 个选择: