[Solved] TreeNode cannot be cast to java.lang.Comparable?

[ad_1] Your problem is that while the generic parameter of TreeNode is Comparable, the class itself is not. Change this: public static class TreeNode<E extends Comparable<E>> { To this: public static class TreeNode<E extends Comparable<E>> implements Comparable<TreeNode<E>> { Or if you don’t require the generic type to also be Comparable: public static class TreeNode<E> implements … Read more

[Solved] Implement a CompareTo in Java [closed]

[ad_1] To use java.util.PriorityQueue, your class need to implement Comparable or specify Comparator when create an instance of PriorityQueue. The elements in queue will be sorted from low value to high value, the element will be evaluated by Comparator.compare or Comparable.compareTo method. In your case, your class Person implements Comparable<Person>, you have to determine the … Read more