Java 比较类对象的变量

Java 比较类对象的变量,java,data-structures,Java,Data Structures,我试图用java实现一个自定义排序的二叉树,但在比较方面遇到了一些问题。我正在尝试实现一个通用树,因此我想做如下工作: public boolean contains(int i){ if(i == currentNode.value) return true; else if (i > currentNode.value) // Go right else if (i < currentNode.value)

我试图用java实现一个自定义排序的二叉树,但在比较方面遇到了一些问题。我正在尝试实现一个通用树,因此我想做如下工作:

public boolean contains(int i){
    if(i == currentNode.value) 
        return true;
    else if (i > currentNode.value) 
        // Go right
    else if (i < currentNode.value)
        // Go left
    ...
}
public boolean contains(Object o){
    if(o == currentNode.value) // value is of Object class
        return true;
    else if (o > currentNode.value) // Problem
        // Go right
    ...
}
 public class MyTree<T extends Comparable<T>>{
 ...
    public boolean contains(TreeNode<T> node, T target)(
            if(node == null) return false;
            if(node.data.equals(target)){
                return true;
            }
            if(node.data.compareTo(target) > 0){//node is greater than target go left
                return contains(node.left, target);
            }else{
                return contains(node.right, target)//node is less than target go right
            }
    }
}
所以我现在的问题是,不可能对对象使用操作符>和<,到目前为止,我还无法想出其他方法来解决这个问题。

如果您使用的是任何操作符,请重写该方法,以进行相等性检查。如果您想比较它们,那么实现接口,或者为它们创建一个新的接口


这个答案不会详细解释如何使用它们,请阅读文档,它们非常详细,并给您一个良好的开端,关于用法,或者搜索示例用法,互联网上到处都是这些用法。

在java中实现Comparable的对象将有一个名为compareTo的方法,该方法返回一个整数。您只需对其进行>和<测试,即:

 int res = comparable.compareTo(other);
 if(res == 0){
   return true;
 } else if(res > 0){
   //go right
   ...
 } else {
   //go left
   ...
 }

这正是java Compariable和泛型的用途

您的对象应该实现:int compareToObject obj方法。如果您正在创建一个二叉搜索树,那么您的contain方法将如下所示:

public boolean contains(int i){
    if(i == currentNode.value) 
        return true;
    else if (i > currentNode.value) 
        // Go right
    else if (i < currentNode.value)
        // Go left
    ...
}
public boolean contains(Object o){
    if(o == currentNode.value) // value is of Object class
        return true;
    else if (o > currentNode.value) // Problem
        // Go right
    ...
}
 public class MyTree<T extends Comparable<T>>{
 ...
    public boolean contains(TreeNode<T> node, T target)(
            if(node == null) return false;
            if(node.data.equals(target)){
                return true;
            }
            if(node.data.compareTo(target) > 0){//node is greater than target go left
                return contains(node.left, target);
            }else{
                return contains(node.right, target)//node is less than target go right
            }
    }
}

物体在本质上是不可比的,苹果比橘子大吗?可比较的对象实现可比较的接口。所以简单的答案是用Comparable和have替换二叉树中对象的每个实例

int comparison = o.compareTo(currentNode.Value)
if(comparison ==0){
   ...
} else if (comparison > 0) {
   ...
} else  {
  // comparison < 0
   ...
}
您可能想了解java泛型,它使您的代码更加类型安全。 你会这样开始的

public class BinaryTree<T extends Comparable> {
    public boolean contains(T target) {
       ....
    }
    ...
}
然后你可以像这样使用它

BinaryTree<Integer> tree = ...
tree.add(1);
tree.add(2);
tree.add("three"); // <-- Syntax error, compiler would fail.
...
Integer first = tree.getFirst();
Integer last = tree.getLast();

您可以对支持Compariable的对象使用Compariable,对不支持Compariable的对象使用System.identityHashCode值进行比较。当然,与哈希代码发生冲突的可能性相对较小,因此您可能希望首先比较类名,缩小哈希空间,然后在仍然发生冲突的情况下使用平底船策略。