简体   繁体   English

用Java排序泛型

[英]Sorting generics in Java

I have to implement a generic AVL tree as homework. 我必须实现一个通用的AVL树作为作业。 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. 由于没有其他限制或信息,因此我首先想到的是假设Key始终是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. 因此,我认为最好的解决方案包括强制执行Key实现比较器时传递的任何内容或类似的东西(我从未真正与Comparator一起工作过,只是猜测),然后使用该比较器比较Key而不是使用==,<,>和!=运算符。 However, I have no idea on how to do it. 但是,我不知道如何去做。 Any hints? 有什么提示吗?

Thanks in advance. 提前致谢。

Try public class AVL<Key extends Comparable<Key>,Elem>; 尝试使用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 . 并使用Comparable<T>接口所需的并且由Integer实现的compareTo()方法。

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) . 标准Java API中的SortedMapSortedSet实现使用Comparator<Key>并调用其compare(k1, k2)方法,或者假定键实现Comparable<Key> ,然后调用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.) (EnumMap / EnumSet不支持,因为它们仅支持按声明顺序对枚举值进行内置排序。)

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. Comparable方法要求必须始终以相同的方式对键进行排序,并且将用于具有规范顺序(例如整数)的键(您要在其中使用此顺序)。

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 (eg Locale-based), using a Collator (this is a class implementing Comparator). Comparator方法更加灵活,因为您可以将相同的键对象用于顺序不同的不同地图,并且可以将其用于无法控制或没有规范顺序的键(例如List) ,树/图形等。您还可以使用它使用整理器(这是实现Comparator的类),按照除纯unicode值(例如,基于语言环境)之外的其他条件对字符串键进行排序。

Both require a total order on your keys, but I suppose this is necessary for your AVL tree, too. 两者都需要对键进行总排序,但是我想这对于您的AVL树也是必需的。

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. 这是一个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);
         }
     };
 }

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

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