I am trying to implement a binary tree which has heaps as nodes. But I couldn't figure out why this error shows up.
First, these are the classes:
BinaryTree:
public class MyBinarySearchTree<E extends Comparable<E>> implements Serializable{...}
Heap:
public class MyHeap<E extends Comparable<E>> implements Comparable<E>{...}
ValueOccurance:
public class ValueOccurance<E extends Comparable<E>> implements Comparable<E> {
private E data;
private int occur;
#Override
public int compareTo(E o) {
return data.compareTo(o);
}
}
So , here is the problem: In main I can't create x and y.
MyHeap<ValueOccurance<Integer>> x = new MyHeap<>();
MyBinarySearchTree<ValueOccurance<Integer>> y = new MyBinarySearchTree<>();
Can you please tell me where is my fault and a little explanation too?
Regards.
MyHeap and ValueOccurence need to be comparable to instances of themselves, not instances of E.
Note the differences between the following and your code (highlighted below):
public static class MyBinarySearchTree<E extends Comparable<E>> implements Serializable{ };
public static class MyHeap<E extends Comparable<E>> implements Comparable<MyHeap<E>>{
^^^^^^^^^
public int compareTo(MyHeap<E> other) {
return 0;
}
}
public class ValueOccurance<E extends Comparable<E>> implements Comparable<ValueOccurance<E>> {
^^^^^^^^^^^^^^^^^
private E data;
private int occur;
#Override
public int compareTo(ValueOccurance<E> o) {
^^^^^^^^^^^^^^^^^^^
return data.compareTo(o.data);
^^^^^^
}
}
public static void main(String[] args) throws IOException
{
MyHeap<ValueOccurance<Integer>> x = new MyHeap<>();
MyBinarySearchTree<ValueOccurance<Integer>> y = new MyBinarySearchTree<>();
}
ValueOccurance<E> is Comparable<E>, not Comparable<ValueOcurrence<E>>. i.e. it cannot compare instances of itself.
Related
I'm having issues right now trying to model a binary search tree with generic data types. I will eventually be reading in string values and inserting them into the binary tree, hence the string declaration in the Nodez class. The Nodez class is a class I defined to declare nodes to pass to the search tree. The string value will be an attribute of this class. The BSTree is based off a class defined as the following:
public class BSTree<E extends Comparable<E>> implements BSTreeAPI<E>
My issue lies in the main block of code. The error occurs when I try to insert an instance of the Nodez class. The exact error here states: "incompatible types: Nodez cannot be converted to Comparable"
I've spent a good amount of time trying to debug this but I am not so great with generics?
Any suggestions please? Thanks!
package twotreesanalyzer;
import java.io.IOException;
import java.io.PrintStream;
import java.util.function.Function;
public class TwoTreesAnalyzer
{
public static class Nodez <E extends Comparable<E>> {
public String x;
public E node;
public String get(){
return x;
}
}
public static void main(String[] args) throws AVLTreeException, BSTreeException, IOException
{
Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);
BSTree bstTest = new BSTree();
Nodez e1 = new Nodez();
e1.x = "fresh";
bstTest.insert(e1);
System.out.println(bstTest.inTree(e1.get()));
}
}
Right now your BSTree is trying to compare your nodez objects, if this is how you want it to function you need to implement Comparible on your Nodez class. I fixed it up quickly with a collections tree as an example.
public static class Nodez <E extends Comparable<E>> implements Comparable<Nodez<E>>{
public String x;
public E node;
public String get(){
return x;
}
#Override
public int compareTo(Nodez<E> node) {
return node.x.compareTo(x);
}
}
public static void main(String[] args) throws IOException
{
Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);
TreeSet<Nodez<String>> bstTest = new TreeSet<>();
Nodez<String> e1 = new Nodez<>();
e1.x = "fresh";
bstTest.add(e1);
System.out.println(bstTest.contains(e1));
}
However I think you were going for the node to be able to accept any generic type that's comparable in which case it should be ordered a bit more like this:
public static class Nodez <E extends Comparable<E>> implements Comparable<Nodez<E>>{
public E x;
public Nodez<E> node;
public E get(){
return x;
}
#Override
public int compareTo(Nodez<E> node) {
return node.x.compareTo(x);
}
}
public static void main(String[] args) throws IOException
{
Function<String, PrintStream> printUpperCase = x -> System.out.printf("%S", x);
TreeSet<Nodez<String>> bstTest = new TreeSet<>();
Nodez<String> e1 = new Nodez<>();
e1.x = "fresh";
bstTest.add(e1);
System.out.println(bstTest.contains(e1));
}
i get a Java type mismatch at the following code. For a better reading i skipped the class bodies.
class Main{
//interface
public interface HasDeletedFlag {
public boolean deleted_flag();
}
//classes
public abstract class Entity<? extends Entity.Factory<E, ?>>> {
...
}
public class Select{
...
public <E extends Entity<? extends Entity.Factory<E, ?>>> List<E> search (E.Factory<E,?> result_factory){
...
}
}
public static class Person extends Entity<Person.$.Factory>
implements HasDeletedFlag {
...
public static class $ {
public static class Factory
extends Entity.Factory<Person,Factory>{
...
}
}
}
static class Database{
...
public final Person.$.Factory person = new Person.$.Factory();
}
//method
private <E extends Entity<?> & HasDeletedFlag> List<E> findEntity(String id){
...
Database db = new Database();
List<E> objects = new ArrayList<E>();
Select query = new Select();
objects = query.<Person>search(db.person);
}
}
In the following line i get the class mismatch error:
objects = query.<Person>search(db.person);
If i add a class cast like (List<E>) the error disappears, but in my understanding the type Person matches the type E.
Greetings
Goetz
I have a project I'm working on and this is what I have started with:
public class Pair<T extends Comparable, E extends Comparable> implements Comparable{
private E e;
private T t;
public int compareTo(Pair arg0) {
// TODO Auto-generated method stub
return 0;
}
}
I need to use this class to sort ordered pairs in ascending order. If the first ones are equal, then it should sort by the send point.
Could you guys please help me get a start on this?
In your class definition, your T and E generics lack the comparison against themselves. This also happens for your Pair class. The definition of the class should be:
public class Pair<T extends Comparable<T>, E extends Comparable<E>> implements Comparable<Pair<T, E>> {
}
Now you can define how to compare the pair. Here's an example:
public class Pair<T extends Comparable<T>, E extends Comparable<E>> implements Comparable<Pair<T, E>> {
private E e;
private T t;
public int compareTo(Pair<T, E> pair) {
int result = t.compareTo(pair.t);
return (result == 0) ? e.compareTo(pair.e) : result;
}
}
Let's say I have:
public class Components<T> extends TupleList<Class<T>, String> {
private static final long serialVersionUID = 1L;
public void add(Class<T> classe, String name) {
this.add(new Tuple<Class<T>, String>(classe, name));
}
}
I'd like to be able to do the following:
Components<IFilter> engines=new Components<IFilter>(){{
add(FilterEngineIdentity.class, "Identity");
}};
where FilterEngineIdentity implements IFilter. How would I achieve this without binding my class Components to more specific class definitions?
Edit: It works! See my test added on the bottom.
Would the following work?
Class<? extends T>
Whole class:
public class Components<T> extends TupleList<Class<?extends T>, String> {
private static final long serialVersionUID = 1L;
public void add(Class<? extends T> classe, String name) {
this.add(new Tuple<Class<? extends T>, String>(classe, name));
}
}
The test (compiles without problems on my machine, Java 1.7 but I didn't use the <> so it should work fine with other versions):
public interface Interface {}
public class Test<T>
{
public void add(Class<? extends T> x)
{
}
public static void x()
{
Test<Interface> t = new Test<Interface>();
t.add(Implementation.class);
}
public static class Implementation implements Interface{}
}
Now that I understand what you're looking for, I think this is more clear and typesafe:
public interface Interface {
}
public class Test<T> {
public <K extends T> void add(Class<K> x) {
}
public static void x() {
Test<Interface> t = new Test<Interface>();
t.add(Implementation.class);
}
public static class Implementation implements Interface {
}
}
class Collator<S extends Stream<E extends Comparable<E>>> {
S s;
E e;
public <S> Collator(List<S> streams){
s = streams.get(0);
e = s.read();
}
public <E> E next(){
return e;
}
}
interface Stream<E extends Comparable<E>>{
public E read();
}
class Record implements Comparable<Record>{
public Integer time;
public int compareTo(Record r){
return this.time.compareTo(r.time);
}
}
Especially 1st line:
class Collator<S extends Stream<E extends Comparable<E>>>
I expect to say:
Define a collator that works on Streams of Entries where each Entry implements comparable.
you miss-qualified the generic parameters
class Collator<S extends Stream<E>,E extends Comparable<E>> {
S s;
E e;
public Collator(List<S> streams){
s = streams.get(0);
e = s.read();
}
public E next(){
return e;
}
}
interface Stream<E extends Comparable<E>>{
public E read();
}
class Record implements Comparable<Record>{
public Integer time;
public int compareTo(Record r){
return this.time.compareTo(r.time);
}
}
this compiles
in particular the line class Collator<S extends Stream<E>,E extends Comparable<E>> it means a Collator that works on a S that is a Stream of E and E implement Comparable
Some glass ball guessing, since you don't show your error message:
Your constructor and method are declaring new type parameters <E> and <S> which are shadowing the type parameters of your class. Remove them.
Then, E should be a type parameter of your class, too.
class Collator<E extends Comparable<E>, S extends Stream<E>> {
S s;
E e;
public Collator(List<S> streams){
s = streams.get(0);
e = s.read();
}
public E next(){
return e;
}
}
interface Stream<E extends Comparable<E>>{
public E read();
}
class Record implements Comparable<Record>{
public Integer time;
public int compareTo(Record r){
return this.time.compareTo(r.time);
}
}
The problem is E extends Comparable
Define a collator that works on Streams of Entries where each Entry implements comparable of a given type:
public class Collator<T,E extends Comparable<T>, S extends Stream<E>>