I get an error in line with theTree.insert(new File("a", 1));
: The method insert(Comparable) in the type Tree is not applicable for the arguments (File)
.
And when I try to cast as such theTree.insert((Comparable) new File("a", 1));
I get other error: Exception in thread "main" java.lang.ClassCastException: File cannot be cast to java.lang.Comparable at TreeApp.main(tree.java:134)
.
Why can't I pass this object?
My object:
class FileObject
{
public String name;
public int size;
File(String a, int b)
{
this.name = a;
this.size = b;
}
}
You need to implement Comparable<File>
class File implements Comparable<File>
{
public String name;
public int size;
File(String passedName, int passedSize)
{
this.name = passedName;
this.size = passedSize;
}
@Override
public int compareTo(File o) {
//e.g Provide comparable on size
return Integer.valueOf(size).compareTo(o.size);
}
}
References:
Note: Please provide compareTo
implementation in contract with equals
method.