jdk 细粒度锁_使用JDK 8轻松进行细粒度排序
jdk 細(xì)粒度鎖
Java的8的推出流和有用的靜態(tài) / 默認(rèn)的方法比較接口可以很容易地根據(jù)個(gè)人的領(lǐng)域兩個(gè)對象比較“值,而不需要實(shí)現(xiàn)一個(gè)比較(T,T)在其對象的類方法被比較。
我將使用一個(gè)簡單的Song類來幫助演示這一點(diǎn),接下來顯示其Song.java代碼清單。
Song.java
剛剛顯示了清單的Song類缺少一個(gè)compare方法,但是我們?nèi)匀豢梢苑浅]p松地在JDK 8中比較該類的實(shí)例。 根據(jù)剛剛顯示的Song的類定義,可以使用以下代碼對歌曲實(shí)例的List進(jìn)行排序,以按發(fā)行年份,藝術(shù)家以及最終專輯的順序。
按年份,藝術(shù)家和專輯對歌曲列表進(jìn)行排序(按此順序)
/*** 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)方式導(dǎo)入 Comparator和Collectors ,則上面的代碼清單將稍微冗長一些,但是將這些接口和類名稱包括在清單中仍然很簡潔,對于該主題的入門博客文章可能更有用。
在上面的代碼清單中, static default方法Comparator.comparingInt和Comparator.thenComparing用于按年份,然后是藝術(shù)家,最后是唱片,對與基礎(chǔ)List關(guān)聯(lián)的Song流進(jìn)行排序。 該代碼具有很高的可讀性,并且可以基于任意單獨(dú)的訪問器方法進(jìn)行對象比較(以及對這些實(shí)例進(jìn)行排序),而無需顯式指定的Comparator (用于每個(gè)比較的訪問器結(jié)果的自然排序順序)。 請注意,如果需要顯式Comparator ,則可以通過接受Comparator的同名重載方法將其提供給這些static default方法。
下一個(gè)代碼清單是整個(gè)演示類。 它包括剛剛顯示的方法,還顯示了由未排序的歌曲List構(gòu)成的人為示例。
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));} }接下來顯示運(yùn)行上述代碼的輸出,并在使用排序代碼后列出新排序的Song 。 值得注意的是,此stream.sorted()操作不會(huì)更改原始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在接口中引入了流以及默認(rèn)方法和靜態(tài)方法(在這種情況下,尤其是在Comparator上)使您可以輕松地按期望的順序逐個(gè)字段地比較兩個(gè)對象,而無需使用任何顯式的Comparator而是預(yù)先構(gòu)建了static default方法Comparator接口(如果要比較的字段具有所需的自然順序)。
翻譯自: https://www.javacodegeeks.com/2018/01/easy-fine-grained-sorting-jdk-8.html
jdk 細(xì)粒度鎖
總結(jié)
以上是生活随笔為你收集整理的jdk 细粒度锁_使用JDK 8轻松进行细粒度排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漂流瓶子电脑版下载(漂流瓶pc下载)
- 下一篇: 海康威视监控设置图解(海康威视监控配置教