Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Trying to somehow Compare Generics

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to somehow Compare Generics

    Heres the thing, I have a class call HoldQueue it needs to hold a list of another class LeftistHeaps which will be a heap of another class TaskObject. LeftistHeap has to be defined as generic, but when implemented in HoldQueue is when it will be given TaskObject as the "data type" its storing. The problem lies in that I nee to make sure when I use a merge method defined in LeftistHeap that the root is highest priority out of the two heaps to be merged, thus the compare. So far Ive done this:

    public class LeftistHeap<E extends Comparable<? super E>> {
    private HeapNode<E> root;
    private int count;

    public LeftistHeap() {
    root = null;
    }

    public void merge(LeftistHeap<E> newHeap) {
    if(this == newHeap) // Avoid aliasing problems
    return;

    root = merge(root, newHeap.root);
    newHeap.root = null;
    }

    private HeapNode<E> merge(HeapNode<E> heap1, HeapNode<E> heap2) {
    if(heap2 == null)
    return heap1;
    if(heap1 == null)
    return heap2;
    if(heap1.data.compareTo(heap2.data) < 0)<-------------single compare line
    return mergeSmall(heap1, heap2);
    else
    return mergeSmall(heap2, heap1);
    }

    private HeapNode<E> mergeSmall(HeapNode<E> heap1, HeapNode<E> heap2) {
    if(heap1.left == null) // Single node
    heap1.left = heap2; // Other fields in h1 already accurate
    else {
    heap1.right = merge(heap1.right, heap2);
    if(heap1.left.nullPath < heap1.right.nullPath)
    swapChildren(heap1);

    heap1.nullPath = heap1.right.nullPath + 1;
    }
    return heap1;
    }
    }//end LeftistHeap

    The problem lies in the compare. When I implement LeftistHeap I get an error:

    LeftistHeap<TaskObject> heap = new LeftistHeap<TaskObject>();

    this gives the error:
    Bound mismatch: The type TaskObject is not a valid substitute for the bounded parameter <E extends Comparable<? super E>> of the type LeftistHeap<E>

    Now if I delete <E extends Comparable<? super E> and replace it with just <E> in my LeftistHeap code the single compareTo becomes the issue and becomes a error.

    Is there away to work around this?


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to somehow Compare Generics

    public void merge(LeftistHeap<E> newHeap) {
    if(this == newHeap) // Avoid aliasing problems
    return;

    root = merge(root, newHeap.root);
    newHeap.root = null;
    }
    Ryan, i have a little knowledge, but according to me, in the above code you are using recursive function as root=merge(root,newHeap.root), you are passing two constructors here, as you have only one in the function definition. Better try it, may be i am wrong. Well, there is no matter if you try it.

Similar Threads

  1. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM
  2. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 10:46 AM
  3. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 07:44 PM
  4. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 04:12 AM
  5. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM