0

I want to override the compareTo method. I googled hard but couldn't find anything useful, because my K is not specifically defined (it is not like People(class example) with name, age).

I have file Node.java that I cannot edit.


public class Node<K extends Comparable<K>, V>
{
    public K key;
    public V value;

    public Node<K, V> parent;
    public Node<K, V> left;
    public Node<K, V> right;

    public Node(K key, V value, Node<K, V> parent, Node<K, V> left, Node<K, V> right)
    {
        this.key = key;
        this.value = value;

        this.parent = parent;
        this.left = left;
        this.right = right;
    }
}

and I have the other file Heap.java

public class Heap<K extends Comparable<K>, V> {...}

I want to override compareTo method at Heap.java (inside {..} here!) so I can compare Node's key and sort them.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
임도현
  • 15
  • 4
  • 1
    I don't think you need to override the `compareTo`, you need to **call** the `compareTo` method of each `K`. – Mark Rotteveel Aug 04 '19 at 06:57
  • 4
    A class Foo is Comparable if you can compare a Foo to another Foo. How do you intend to compare a Heap to another Heap? Before "googling hard", read the javadoc of Comparable. – JB Nizet Aug 04 '19 at 06:58
  • 1
    Possible duplicate of [How to implement the Java comparable interface?](https://stackoverflow.com/questions/21626439/how-to-implement-the-java-comparable-interface) – MC Emperor Aug 04 '19 at 06:59
  • `Comparable` would go to the `Node` class, not `Heap`. Else you would compare heaps and not nodes. This has nothing to do with `K` being generic. Since you can not edit `Node`, you want a `Comparator` instead of creating a natural ordering. – Zabuzard Aug 04 '19 at 08:32
  • thank you Mark Rotteveel your right i don't know why but i just can call compareTo and eclipse showed no error! i wasted 4 hours thank you so much!! – 임도현 Aug 04 '19 at 09:35

2 Answers2

2

Instead of overriding compareTo, you can create aComparator<Node> like this:

Comparator<Node<K extends Comparable<K>>, V> comparator =
        Comparator.comparing(node -> node.key);

You can use it for sorting in most library methods, eg in the case of sorted maps, you can pass a Comparator.

Note however that it is a bad idea to have a public key field. It would be bad enough by itself, but if you use it for storing its even worse because you'll mess up the short when someone else changes the field.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
daniu
  • 14,137
  • 4
  • 32
  • 53
0

You can create the comparator as follows if you can provide compareTo inside classes corresponding to K

Comparator<Node<K, V>> comparator = new Comparator<Node<K, V>>() {
    @Override
    public int compare(Node<K, V> node1, Node<K, V> node2) {
        return node1.key.compareTo(node2.key);
    }
};

you can use this comparator anywhere, for example to sort list of nodes, you can simply do Collections.sort(nodes, comparator)

You can also bind K to an interface which gives you method to get value of key, and use it in Heap.java to define your complete logic there itself

Saurabh Garg
  • 39
  • 11