Java 中泛型的排序

发布于 2024-11-01 22:46:57 字数 392 浏览 0 评论 0 原文

我必须实现一个通用的 AVL 树作为作业。它的定义如下:

public class AVL<Key,Elem>;

问题是我假设在某个时候,我必须比较键来决定在节点的哪一侧分配元素。出于本作业的目的,将使用整数作为键。

由于没有给出其他限制或信息,我首先想到假设 Key 始终是一个 Integer。然而,这使得通用的“钥匙”变得多余,我认为这并不是老师们所期望的。因此,我认为最好的解决方案包括强制作为 Key 传递的任何内容实现 Comparator 或类似的东西(我真的从未使用过 Comparator,只是猜测),然后使用该比较器来比较 Key 而不是使用 ==,<,>;和 != 运算符。但是,我不知道该怎么做。有什么提示吗?

提前致谢。

I have to implement a generic AVL tree as homework. It's defined as follows:

public class AVL<Key,Elem>;

The problem is that I assume that at some point, I'll have to compare keys to decide in which side of a node I allocate an element. For the purpose of this homework, Integers will be used as Keys.

Since no other restriction or information about that is given, I first thought of just asuming that Key will always be an Integer. However, that makes the generic "Key" superfluous, and I don't think that's what the teachers expect. So, I think that the best solution involves forcing that whatever that is passed as Key implements a Comparator, or something like that (I've really never worked with Comparator, just guessing), and then using that comparator to compare the Keys instead of using the ==,<,> and != operators. However, I have no idea on how to do it. Any hints?

Thanks in advance.

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

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

发布评论

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

评论(2

画中仙 2024-11-08 22:46:57

尝试 public class AVL,Elem>; 并使用 Comparable 所需的 compareTo() 方法code> 接口,由 Integer 实现。

Try public class AVL<Key extends Comparable<Key>,Elem>; and use the compareTo() method which is required by the Comparable<T> interface and which is implemented by Integer.

木落 2024-11-08 22:46:57

标准 Java API 中的 SortedMapSortedSet 实现使用 Comparator 并调用其 compare(k1, k2) 方法,或者假设键实现了 Comparable,并调用 k1.compareTo(k2)。大多数都提供两者,具体取决于使用哪个构造函数。 (EnumMap/EnumSet 不支持,因为它们仅支持按声明顺序对枚举值进行内置排序。)

Comparable 方法要求键始终以相同的方式排序,并且将用于具有规范排序(如整数)的键,您希望在其中使用此排序。

Comparator 方法更加灵活,因为您可以将相同的键对象用于顺序不同的不同映射,并且可以将其用于您无法控制或无法控制的键具有规范的排序(如列表、树/图等)。您还可以使用 Collat​​or(这是一个实现 Comparator 的类),使用它根据纯 unicode 值之外的其他标准(例如基于区域设置)对字符串键进行排序。

两者都需要键上的总顺序,但我认为这对于您的 AVL 树也是必要的。

这是一个适用于任何可比较对象的 Comparator 实现,因此您可以使用它(可能在内部)作为 Comparable 变体的适配器。 。

 public static <X extends Comparable<X>> Comparator<X> makeComparator() {
     return new Comparator<X>() {
         public int compare(X left, X right) {
             return left.compareTo(right);
         }
     };
 }

The SortedMap and SortedSet implementations in the standard Java API either use a Comparator<Key> and call its compare(k1, k2) method, or assume the keys implement Comparable<Key>, and call k1.compareTo(k2). Most offer both, depending on which constructor is used. (EnumMap/EnumSet don't, as they support only the build-in ordering of the enum values by declaration order.)

The Comparable approach mandates that the keys are always sorted in the same way, and would be used for keys which have a canonical ordering (like integers), where you want to use this ordering.

The Comparator approach is more flexible, since you can use the same key objects for different maps where they are differently ordered, and you can use it for keys over which you have no control, or who don't have a canonical ordering (like List, trees/graphs, etc. You can also use it to sort strings keys by other criteria than the pure unicode value (e.g. Locale-based), using a Collator (this is a class implementing Comparator).

Both require a total order on your keys, but I suppose this is necessary for your AVL tree, too.

Here is a Comparator implementation which works on any comparable objects, so you could use it (maybe internally) as an adapter for the Comparable variant.

 public static <X extends Comparable<X>> Comparator<X> makeComparator() {
     return new Comparator<X>() {
         public int compare(X left, X right) {
             return left.compareTo(right);
         }
     };
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文