日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用JDK 8轻松进行细粒度排序

發布時間:2023/12/3 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用JDK 8轻松进行细粒度排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java的8的推出流和有用的靜態 / 默認的方法比較接口可以很容易地根據個人的領域兩個對象比較“值,而不需要實現一個比較(T,T)在其對象的類方法被比較。

我將使用一個簡單的Song類來幫助演示這一點,接下來顯示其Song.java代碼清單。

Song.java

package dustin.examples.jdk8;/*** Simple class encapsulating details related to a song* and intended to be used for demonstration of JDK 8.*/ public class Song {/** Song title. */private final String title;/** Album on which song was originally included. */private final String album;/** Song's artist. */private final String artist;/** Year song was released. */private final int year;/*** Constructor accepting this instance's title, artist, and release year.** @param newTitle Title of song.* @param newAlbum Album on which song was originally included.* @param newArtist Artist behind this song.* @param newYear Year song was released.*/public Song(final String newTitle, final String newAlbum,final String newArtist, final int newYear){title = newTitle;album = newAlbum;artist = newArtist;year = newYear;}public String getTitle(){return title;}public String getAlbum(){return album;}public String getArtist(){return artist;}public int getYear(){return year;}@Overridepublic String toString(){return "'" + title + "' (" + year + ") from '" + album + "' by " + artist;} }

剛剛顯示了清單的Song類缺少一個compare方法,但是我們仍然可以很容易地在JDK 8中比較該類的實例。 根據剛剛顯示的Song的類定義,以下代碼可用于根據歌曲發行年份,歌手和專輯的順序對歌曲實例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()); }

如果我靜態地導入 Comparator和Collectors ,則上面的代碼清單將稍微冗長一些,但是將這些接口和類名稱包括在清單中仍然很簡潔,對于該主題的入門博客文章可能更有用。

在上面的代碼清單中, static default方法Comparator.comparingInt和Comparator.thenComparing用于按年份,然后是藝術家,最后是唱片,對與基礎List關聯的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 Metallica

JDK 8在接口中引入了流以及默認方法和靜態方法(在這種情況下,尤其是在Comparator上),可以輕松地按期望的順序逐個字段地比較兩個對象,而無需使用任何顯式的Comparator而是預先構建了static default方法。 Comparator界面(如果要比較的字段具有所需的自然順序)。

翻譯自: https://www.javacodegeeks.com/2018/01/easy-fine-grained-sorting-jdk-8.html

總結

以上是生活随笔為你收集整理的使用JDK 8轻松进行细粒度排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。