在 Java Comparator 中比较两个 TreeNode(或 DefaultMutableTreeNode)对象

发布于 2024-09-26 16:00:53 字数 1354 浏览 9 评论 0 原文

今天我的目标非常简单,我正在尝试找出正确的方法来为我的类(扩展了 DefaultMutableTreeNode)实现compareTo(或Comparable)接口。

问题是这样的:假设我有一个代表时代的完美类。我已经编写了一个非常好的compareTo方法(它按照我的意愿工作),我已经用Arrays.sort()对其进行了测试,结果非常好。

现在假设我有一个包含一堆不同对象的 JTree,如下所示:

    new SpecialNode("Zomg a string!"); // add this group of nodes right here
    new SpecialNode(new Time("8:55 PM"));
    new SpecialNode(new SomeTypeYouveNeverHeardOf());

因此,作为一名专业程序员,我立即开始编码,无需任何深思熟虑。这是我的 SpecialNode 类:

class SpecialNode extends DefaultMutableTreeNode implements Comparator<SpecialNode>
{
    public int compareTo(SpecialNode sn)
    {
        // Not only does this not work correctly (read: at all)
        // But, it is sub-par, how do I get the type information out of the userObject
        // So I can cast it correctly and call the correct compareTo method!!
        return this.getUserObject().toString().compareTo(sn.getUserObject().toString());
    }
}

好的,如果您没有阅读评论(承认,您没有阅读);我的问题是,在 SpecialNode 的 CompareTo 方法中,我只能访问 userObject。遗憾的是,我不知道 userObject 曾经是什么,因此,我无法正确地转换它以调用正确的compareTo 方法!

这确实很痛苦,因为我已经在所有将添加到我的树中的类中编写了几个非常好的compareTo 方法。那么有人可以帮助一个人并给我一个提示吗?

tl;dr - 如何从 DefaultMutableTreeNode 存储的通用对象中获取类型信息?如果这是不可能的,当我什至不知道它们可能包含什么时,我应该如何比较 SpecialNode 的两个实例!

提前致谢。

My goal is very simple today, I am trying to work out the proper way to implement compareTo (or the Comparable) interface for my class which extends DefaultMutableTreeNode.

The problem is this: Say I have a perfectly good class that represents times. I've already written a perfectly good compareTo method (which works as I desire) that I've tested out with Arrays.sort() with marvelous results.

Now lets say I have a JTree with bunches of different objects, like this:

    new SpecialNode("Zomg a string!"); // add this group of nodes right here
    new SpecialNode(new Time("8:55 PM"));
    new SpecialNode(new SomeTypeYouveNeverHeardOf());

So, being a professional programmer, I immediately start coding without any forethought whatsoever. Here is my SpecialNode class:

class SpecialNode extends DefaultMutableTreeNode implements Comparator<SpecialNode>
{
    public int compareTo(SpecialNode sn)
    {
        // Not only does this not work correctly (read: at all)
        // But, it is sub-par, how do I get the type information out of the userObject
        // So I can cast it correctly and call the correct compareTo method!!
        return this.getUserObject().toString().compareTo(sn.getUserObject().toString());
    }
}

Ok, so if you didn't read the comments (which, admit, you didn't); My problem is that within the compareTo method of SpecialNode, I only have access to the userObject. Sadly, I do not know what the userObject used to be, and as such, I cannot properly cast it to call the correct compareTo method!

This is really a pain, since I've already written several perfectly good compareTo methods in all of the classes which will be added to my tree. So can someone help a guy out and drop me a hint?

tl;dr - How do I get type information out of an generic object that DefaultMutableTreeNode stores? If that is not possible, how should I go about comparing two instances of SpecialNode when I don't even know what they may contain!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-10-03 16:00:53

我假设您不能为每种类型的数据都有一个 TreeNode。如果类型不同,在您的用例中进行比较是否有意义?

想法:

  1. SimpleNode 可以知道所有可能的类型并执行instanceof 并强制转换为Comparable 的正确类型吗?这是我几年前处理它的方式。

  2. 您对未经检查的警告有何看法?在使用 JList 之前我也遇到过类似的问题,并且无法让编译器满意(我放弃了 swing 默认模型以使我的生活更轻松)。也许其他人可以改进这个答案?

    class SpecialNode>扩展 DefaultMutableTreeNode 
                                              实现 Comparable
    {
      T 类型化用户对象;
      特殊节点(T t)
      {
         this.typedUserObject = t;
         设置用户对象(t);
      }
    
    
      public int CompareTo(SpecialNode 节点)
      {
          if(typedUserObject.getClass().isInstance(node.typedUserObject))
          {
              T otherObj = (T) 节点.typedUserObject;
              返回 typedUserObject.compareTo(otherObj);
          }
          别的
          {
              //如果它们不是同一类型你会怎么做?
              返回-1;
          }
      }
    
    
    }
    

编辑:如果您知道它们应该是相同的类型 - 消除检查

class SpecialNode<T extends Comparable<T>> extends DefaultMutableTreeNode
                                          implements Comparable<SpecialNode<T>>
{
  T typedUserObject;
  SpecialNode(T t)
  {
     this.typedUserObject = t;
     setUserObject(t);
  }


  public int compareTo(SpecialNode<T> node)
  {
     return typedUserObject.compareTo(node.typedUserObject);
  }    
}

如果您不希望节点本身中的代码(我不认为我会),您可以创建一个单独的类实现比较器>

I'm assuming you can't have a TreeNode for each type of data. Does it make sense in your use case to do comparisons if the type is different?

Ideas:

  1. Can SimpleNode know all of the possible types and do instanceof and casts to the correct type for Comparable? This is the way I would have handled it a few years ago.

  2. How do you feel about unchecked warnings? I had a similar problem before using a JList and couldn't quite make the compiler happy (I gave up the swing default model to make my life easier). Maybe someone else could improve on this answer?

    class SpecialNode<T extends Comparable<T>> extends DefaultMutableTreeNode 
                                              implements Comparable<SpecialNode>
    {
      T typedUserObject;
      SpecialNode(T t)
      {
         this.typedUserObject = t;
         setUserObject(t);
      }
    
    
      public int compareTo(SpecialNode node)
      {
          if(typedUserObject.getClass().isInstance(node.typedUserObject))
          {
              T otherObj = (T) node.typedUserObject;
              return typedUserObject.compareTo(otherObj);
          }
          else
          {
              //What are you going to do if they're not the same type?
              return -1;
          }
      }
    
    
    }
    

Edit: If you know they should be the same type - eliminates check

class SpecialNode<T extends Comparable<T>> extends DefaultMutableTreeNode
                                          implements Comparable<SpecialNode<T>>
{
  T typedUserObject;
  SpecialNode(T t)
  {
     this.typedUserObject = t;
     setUserObject(t);
  }


  public int compareTo(SpecialNode<T> node)
  {
     return typedUserObject.compareTo(node.typedUserObject);
  }    
}

If you don't want the code in the node itself (I don't think I would), you might create a separate class that implements Comparator<SpecialNode<T>>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文