0

I want to merge to two linkedLists in JAVA but when i want to compare the data h1.data <= h2.data, I am getting that operator <= cannot be applied to 'E' 'E'.

Here is my generic class SLLNode<E>

public class SLL<E extends Comparable<E>>  {
    private SLLNode<E> first;
    public SLL(){
        this.first=null;
    }

Now when I want to implement the merge method

public SLLNode<E> merge (SLLNode<E> h1, SLLNode<E> h2){
    if (h1 == null)
    {
        return h2;
    }
    if (h2 == null){
        return h1;
    }

    SLLNode<E> mergedHead = null;
    if(h1.data<=h2.data)

I'm getting the error listed above.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Provide the code for class `E` – Spectric Oct 30 '20 at 21:15
  • 1
    You probably want to use `.compareTo()`, rather than `<=`. The comparison operators only tend to work on primitives, in Java, whereas the `Comparable` interface that objects of type `E` must implement guarantees that the object will have the `compareTo()` method. e.g. `if (h1.data.compareTo(h2.data) <= 0)` – Green Cloak Guy Oct 30 '20 at 21:17
  • @GreenCloakGuy That worked, you can use it as an Answer. – Filip Gorgievski Oct 30 '20 at 21:21

0 Answers0