为什么 Java Collections Framework 提供两种不同的排序方式? [英] Why does the Java Collections Framework offer two different ways to sort?

查看:34
本文介绍了为什么 Java Collections Framework 提供两种不同的排序方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个要排序的元素列表,Java 提供了两种方法来解决这个问题.

If I have a list of elements I would like to sort, Java offers two ways to go about this.

例如,假设我有一个 Movie 对象列表,我想按标题对它们进行排序.

For example, lets say I have a list of Movie objects and I’d like to sort them by title.

我可以做到这一点的一种方法是调用静态 java.util.Collections.sort() 方法的单参数版本,并将我的电影列表作为单个参数.所以我会调用 Collections.sort(myMovieList).为了使其工作,必须声明 Movie 类以实现 java.lang.Comparable 接口,并且必须在此类内部实现所需的方法 compareTo().

One way I could do this is by calling the one-argument version of the static java.util.Collections.sort( ) method with my movie list as the single argument. So I would call Collections.sort(myMovieList). In order for this to work, the Movie class would have to be declared to implement the java.lang.Comparable interface, and the required method compareTo( ) would have to be implemented inside this class.

另一种排序方法是调用静态 java.util.Collections.sort() 方法的两个参数版本,并使用电影列表和 java.util.Comparator 对象作为参数.我会调用 Collections.sort(myMovieList, titleComparator).在这种情况下,Movie 类不会实现 Comparable 接口.相反,在构建和维护电影列表本身的主类中,我将创建一个实现 java.util.Comparator 接口的内部类,并实现一个所需的方法 compare().然后我会创建这个类的一个实例并调用 sort() 的两个参数版本.第二种方法的好处是您可以创建无限数量的这些内部类 Comparator,因此您可以以不同方式对对象列表进行排序.例如,在上面的示例中,您可以使用另一个 Comparator 按电影制作年份排序.

Another way to sort is by calling the two-argument version of the static java.util.Collections.sort( ) method with the movie list and a java.util.Comparator object as it’s arguments. I would call Collections.sort(myMovieList, titleComparator). In this case, the Movie class wouldn’t implement the Comparable interface. Instead, inside the main class that builds and maintains the movie list itself, I would create an inner class that implements the java.util.Comparator interface, and implement the one required method compare( ). Then I'd create an instance of this class and call the two-argument version of sort( ). The benefit of this second method is you can create an unlimited number of these inner class Comparators, so you can sort a list of objects in different ways. In the example above, you could have another Comparator to sort by the year a movie was made, for example.

我的问题是,为什么要在 Java 中学习两种排序方式,因为 Collections.sort() 的双参数版本可以完成第一个单参数版本所做的所有事情,但具有能够排序的额外好处列表的元素基于几个不同的标准?在编码时必须记住的事情会少一件事.您需要了解 Java 中排序列表的一种基本机制.

My question is, why bother to learn both ways to sort in Java, when the two-argument version of Collections.sort( ) does everything the first one-argument version does, but with the added benefit of being able to sort the list’s elements based on several different criteria? It would be one less thing to have to keep in your mind while coding. You’d have one basic mechanism of sorting lists in Java to know.

推荐答案

一个是为了简洁,应该是一个常见的案例(Effective Java 2nd Edition,Item 12:考虑实现 Comparable).正如您所指出的,另一个是为了灵活性和通用性.

One is for conciseness for what should be a common case (Effective Java 2nd Edition, Item 12: Consider implementing Comparable). The other, as you noted, is for flexibility and general-purposeness.

这篇关于为什么 Java Collections Framework 提供两种不同的排序方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆