使用JDK 8轻松进行细粒度排序
Java的8的推出流和有用的靜態(tài) / 默認的方法比較接口可以很容易地根據個人的領域兩個對象比較“值,而不需要實現(xiàn)一個比較(T,T)在其對象的類方法被比較。
我將使用一個簡單的Song類來幫助演示這一點,接下來顯示其Song.java代碼清單。
Song.java
剛剛顯示了清單的Song類缺少一個compare方法,但是我們仍然可以很容易地在JDK 8中比較該類的實例。 根據剛剛顯示的Song的類定義,以下代碼可用于根據歌曲發(fā)行年份,歌手和專輯的順序對歌曲實例List進行排序。
按年份,藝術家和專輯對歌曲排序(按此順序)
/*** Returns a sorted version of the provided List of Songs that is* sorted first by year of song's release, then sorted by artist,* and then sorted by album.** @param songsToSort Songs to be sorted.* @return Songs sorted, in this order, by year, artist, and album.*/ private static List<Song> sortedSongsByYearArtistAlbum(final List<Song> songsToSort) {return songsToSort.stream().sorted(Comparator.comparingInt(Song::getYear).thenComparing(Song::getArtist).thenComparing(Song::getAlbum)).collect(Collectors.toList()); }如果我靜態(tài)地導入 Comparator和Collectors ,則上面的代碼清單將稍微冗長一些,但是將這些接口和類名稱包括在清單中仍然很簡潔,對于該主題的入門博客文章可能更有用。
在上面的代碼清單中, static default方法Comparator.comparingInt和Comparator.thenComparing用于按年份,然后是藝術家,最后是唱片,對與基礎List關聯(lián)的Song流進行排序。 該代碼具有很高的可讀性,并且可以基于任意單獨的訪問器方法進行對象比較(以及對這些實例進行排序),而無需顯式指定的Comparator (用于每個比較的訪問器結果的自然排序順序)。 請注意,如果需要顯式Comparator ,則可以通過接受Comparator的同名重載方法將其提供給這些static default方法。
下一個代碼清單是整個演示類。 它包括剛剛顯示的方法,還顯示了由未排序的歌曲List構成的人為示例。
FineGrainSortingDemo.java
package dustin.examples.jdk8;import static java.lang.System.out;import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors;/*** Demonstration of easy fine-grained sorting in JDK 8 via* stream support for sorting and Comparator's static and* default method implementations.*/ public class FineGrainSortingDemo {/*** Construct List of {@code Song}s.* * @return Instances of {@code Song}.*/private static List<Song> generateSongs(){final ArrayList<Song> songs = new ArrayList<>();songs.add(new Song("Photograph","Pyromania","Def Leppard",1983));songs.add(new Song("Hysteria","Hysteria","Def Leppard",1987));songs.add(new Song("Shout","Songs from the Big Chair","Tears for Fears",1984));songs.add(new Song("Everybody Wants to Rule the World","Songs from the Big Chair","Tears for Fears",1985));songs.add(new Song("Head Over Heels","Songs from the Big Chair","Tears for Fears",1985));songs.add(new Song("Enter Sandman","Metallica","Metallica",1991));songs.add(new Song("Money for Nothing","Brothers in Arms","Dire Straits",1985));songs.add(new Song("Don't You (Forget About Me)","A Brass Band in African Chimes","Simple Minds",1985));return songs;}/*** Returns a sorted version of the provided List of Songs that is* sorted first by year of song's release, then sorted by artist,* and then sorted by album.** @param songsToSort Songs to be sorted.* @return Songs sorted, in this order, by year, artist, and album.*/private static List<Song> sortedSongsByYearArtistAlbum(final List<Song> songsToSort){return songsToSort.stream().sorted(Comparator.comparingInt(Song::getYear).thenComparing(Song::getArtist).thenComparing(Song::getAlbum)).collect(Collectors.toList());}/*** Demonstrate fine-grained sorting in JDK 8.** @param arguments Command-line arguments; none expected.*/public static void main(final String[] arguments){final List<Song> songs = generateSongs();final List<Song> sortedSongs = sortedSongsByYearArtistAlbum(songs);out.println("Original Songs:");songs.stream().forEach(song -> out.println("\t" + song));out.println("Sorted Songs");sortedSongs.forEach(song -> out.println("\t" + song));} }接下來顯示運行上述代碼的輸出,并在使用排序代碼后列出新排序的Song 。 值得注意的是,此stream.sorted()操作不會更改原始List (它作用于流而不是List )。
Original Songs:'Photograph' (1983) from 'Pyromania' by Def Leppard'Hysteria' (1987) from 'Hysteria' by Def Leppard'Shout' (1984) from 'Songs from the Big Chair' by Tears for Fears'Everybody Wants to Rule the World' (1985) from 'Songs from the Big Chair' by Tears for Fears'Head Over Heels' (1985) from 'Songs from the Big Chair' by Tears for Fears'Enter Sandman' (1991) from 'Metallica' by Metallica'Money for Nothing' (1985) from 'Brothers in Arms' by Dire Straits'Don't You (Forget About Me)' (1985) from 'A Brass Band in African Chimes' by Simple Minds Sorted Songs'Photograph' (1983) from 'Pyromania' by Def Leppard'Shout' (1984) from 'Songs from the Big Chair' by Tears for Fears'Money for Nothing' (1985) from 'Brothers in Arms' by Dire Straits'Don't You (Forget About Me)' (1985) from 'A Brass Band in African Chimes' by Simple Minds'Everybody Wants to Rule the World' (1985) from 'Songs from the Big Chair' by Tears for Fears'Head Over Heels' (1985) from 'Songs from the Big Chair' by Tears for Fears'Hysteria' (1987) from 'Hysteria' by Def Leppard'Enter Sandman' (1991) from 'Metallica' by MetallicaJDK 8在接口中引入了流以及默認方法和靜態(tài)方法(在這種情況下,尤其是在Comparator上),可以輕松地按期望的順序逐個字段地比較兩個對象,而無需使用任何顯式的Comparator而是預先構建了static default方法。 Comparator界面(如果要比較的字段具有所需的自然順序)。
翻譯自: https://www.javacodegeeks.com/2018/01/easy-fine-grained-sorting-jdk-8.html
總結
以上是生活随笔為你收集整理的使用JDK 8轻松进行细粒度排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓无线传输文件到苹果(安卓无线传输文件
- 下一篇: 阿里ddos流量多大就会堵塞了(阿里dd