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

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

问题描述

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



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



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



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



我的问题是,为什么还要学习两种排序方式在Java中,当Collections.sort()的双参数版本执行第一个单参数版本的所有功能时,还有一个额外的好处是能够根据几个不同的标准对列表的元素进行排序?在编码的时候,一定要记住一点。你将有一个基本的机制,在Java中排序列表。

解决方案

一个是为了简洁, case( Effective Java第2版,第12项:考虑实现 Comparable )。



相关问题




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

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

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.

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.

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.

解决方案

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.

Related questions

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

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