简体   繁体   English

如何覆盖 compareTo 方法?

[英]How do I override the compareTo method?

I want to override the compareTo method.我想覆盖compareTo方法。 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).我用谷歌搜索但找不到任何有用的东西,因为我的 K 没有明确定义(它不像人(类示例)有名字、年龄)。

I have file Node.java that I cannot edit.我有无法编辑的文件 Node.java。


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我有另一个文件 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.我想覆盖 Heap.java 中的 compareTo 方法(在{..}里面!),这样我就可以比较 Node 的键并对它们进行排序。

Instead of overriding compareTo , you can create a Comparator<Node> like this: 您可以像这样创建Comparator<Node> ,而不是覆盖compareTo

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. 您可以使用它在大多数库方法中进行排序,例如,对于已排序的地图,可以通过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. 它本身就够糟糕的了,但是如果用它来存储它甚至更糟,因为当其他人更改字段时,您会搞砸。

If you want object to be used as a natural sorting key then you use comparable on that object, ie Heap implement Comparable will make you override it then you can use your heap object in node. 如果希望将对象用作自然排序键,则可以在该对象上使用可比对象,即,堆实现Comparable将使您覆盖它,然后可以在节点中使用堆对象。 And if you want your heap object to sort comparable object best way would be to look at the implementation in Collections class. 而且,如果您希望堆对象对可比较的对象进行排序,最好的方法是查看Collections类中的实现。

You can create the comparator as follows if you can provide compareTo inside classes corresponding to K 如果可以在与K对应的类中提供compareTo则可以按如下方式创建比较器

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) 您可以在任何地方使用此比较器,例如对节点列表进行排序,只需执行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 您还可以将K绑定到一个接口,该接口为您提供获取键值的方法,并在Heap.java中使用它自己定义完整的逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM