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

jdk 细粒度锁

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());
}

如果我以静态方式导入 ComparatorCollectors ,则上面的代码清单将稍微冗长一些,但是将这些接口和类名称包括在清单中仍然很简洁,对于该主题的入门博客文章可能更有用。

在上面的代码清单中, 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 细粒度锁

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/334289.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言命名规则_C语言的基本数据类型及变量

学习目标了解C语言的基本数据类型了解变量的基本概念了解变量的使用方法了解了变量的命名方法了解格式占位符了解变量的输出了解C语言程序的基本数据类型及概念的使用方法擦在C语言编程中&#xff0c;系统定义了多种数据类型&#xff0c;本节将讲解基本数据类型的分类。基本数据…

linux socket默认超时时间设置,Socket中如何设置连接超时 (转)

Socket中如何设置连接超时 (转)Socket中如何设置连接超时AntGhazi/2001.12.14 主页&#xff1a;antghazi.yeah把CSDN与中文翻了底朝天&#xff0c;也没找到如何设置socket的连接超时的满意方法&#xff0c;问此问题的兄弟已有一大堆&#xff0c;这里偶就讲一下win下如何设置soc…

Linux 常用的软件包管理器/软件包管理工具详解

文章目录RPM 是什么&#xff1f;应用于哪些系统RPM 的前端工具有哪些RPM 包命名规范RPM 安装软件的默认路径RPM 安装原理图RPM 命令详解YUM 是什么&#xff1f;应用于哪些系统YUM 原理说明主要特点YUM 和 RPM 的区别YUM 命令详解DNF 是什么应用于哪些系统DNF 命令详解APT 是什么…

java world_Java World中的GraphQL简介

java world许多人认为GraphQL仅适用于前端和JavaScript&#xff0c;它在Java等后端技术中不占优势&#xff0c;但事实确实如此。 还经常将GraphQL与REST进行比较&#xff0c;但是这种比较是否合理&#xff1f; 首先&#xff0c;让我开始回答其中最重要的问题。 什么是GraphQL…

快速排序 动图_Java十大排序算法最强总结

看到一篇很不错的文章&#xff0c;不多说&#xff0c;看吧排序算法说明0.1 排序的定义对一序列对象根据某个关键字进行排序。0.2 术语说明稳定&#xff1a;如果a原本在b前面&#xff0c;而ab&#xff0c;排序之后a仍然在b的前面&#xff1b;不稳定&#xff1a;如果a原本在b的前…

linux安装2870无线网卡,ubuntu15.04安装usb无线网卡

一般这种无线网卡都是联fake芯片&#xff0c;我使用的ralin(你懂的k)的usb无线1150 M。你去找lei凌官网找不到&#xff0c;只能去找芯片类型的制造者&#xff0c;所以只能去联Fake官网查询下载对应型号。1、我是这样子查看型号的&#xff0c;找到通过驱动软件检测并已经安装成功…

Linux 应用程序的源码包如何安装?

文章目录configuremakemake install关于文件 configure 的简单介绍其它命令简介C 语言开发的应用程序的源码包常以 .tar.gz 为扩展名&#xff0c;并且这些源码包通常使用 GNU 的 AUTOCONF 和 AUTOMAKE 生成编译配置文件&#xff0c;我们拿到这样的软件包后&#xff0c;执行下面…

exec su-exec_WildFly Kubernetes exec探针

exec su-exec活动性和就绪性探针会告诉Kubernetes&#xff0c;某个Pod是否正在运行并准备进行一些工作。 企业应用程序可以通过HTTP探测应用程序的状态。 如果没有暴露HTTP端点&#xff0c;Kubernetes也可以通过执行命令进行探测。 WildFly附带了有用的jboss-cli.sh 。 此CLI检…

feignclient注解使用_从 Feign 使用注意点到 RESTFUL 接口设计规范

最近项目中大量使用了Spring Cloud Feign来对接http接口&#xff0c;踩了不少坑&#xff0c;也产生了一些对RESTFUL接口设计的想法&#xff0c;特此一篇记录下。SpringMVC的请求参数绑定机制了解Feign历史的朋友会知道&#xff0c;Feign本身是Netflix的产品&#xff0c;Spring …

Linux 的发行版 Ubuntu 的发展简史

Ubuntu&#xff08;又称乌班图&#xff09;是一个是基于 Debian GNU/Linux 的以桌面应用为主的免费开源的 GNU/Linux 操作系统&#xff0c;由全球化的专业开发团队 Canonical 公司打造的。 Ubuntu Linux 是由南非人马克沙特尔沃思(Mark Shuttleworth)创办的基于 Debian Linux的…

linux实验报告五gcc编译,Linux之GCC命令 -- 解析GCC编译的四个过程

在Linux下进行C语言编程&#xff0c;必然要采用GNU GCC来编译C源代码生成可执行程序。Gcc指令的一般格式为&#xff1a;Gcc [选项] 要编译的文件 [选项] [目标文件]。其中&#xff0c;目标文件可缺省&#xff0c;Gcc默认生成可执行的文件名为&#xff1a;编译文件.out看一下经典…

python创建虚拟环境命令_Python创建虚拟环境报错

我使用命令&#xff1a;mkvirtualenv -p python3 test1,创建虚拟环境&#xff0c;然后报错如下()&#xff0c;虚拟机是用nat模式的: Exception: Traceback (most recent call last): File "/home/python/.virtualenvs/test11/share/python-wheels/urllib3-1.13.1-py2.py3-…

java7和java8切换_切换表达式到Java吗?

java7和java8切换已创建一个标题为“ Java语言的开关表达式”的JEP草案 。 当前的“摘要”状态为&#xff1a;“扩展switch语句&#xff0c;以便可以将其用作语句或表达式&#xff0c;并改善switch处理null的方式。 这些将简化日常编码&#xff0c;并为在switch使用模式匹配做好…

Xenix 操作系统的简史

简介 Xenix 是一种UNIX操作系统&#xff0c;可在个人电脑及微型计算机上使用。该系统由微软公司在1979年从美国电话电报公司获得授权&#xff0c;为Intel处理器所开发。后来&#xff0c;圣克鲁兹作业公司&#xff08;SCO&#xff09;收购了其独家使用权&#xff0c;自那以后&a…

servlet 配置 使用_配置HTTPS以与Servlet一起使用

servlet 配置 使用要配置Java EE应用程序以通过HTTPS进行通信&#xff0c;需要在web.xml文件中使用几行XML。 web.xml文件位于项目的WEB-INF目录中&#xff0c;通常在IDE生成Java EE Web应用程序时自动创建。 如果不是&#xff0c;您可以自己创建它。 HTTPS的动机 为Web应用程…

python写接口测试代码_python写运单接口测试(增改查)完整代码

importrequestsimportjsonfrom urllib importparseclassHttpWayBillRquest:运单的增改查 defaccess_token(self):获取tokenurl http://xxxxxxxxx.comusername 12333password 12334566res_json requests.get(url, auth(username, password)).json()print(access_token的结果为&a…

linux终端cd未找到命令,为什么`which`命令不能用于`cd`?我也找不到`cd`的可执行文件!...

问题描述我尝试了which cd&#xff0c;它没有给出路径&#xff0c;而是返回退出代码1(用echo $?检查)。 coreutil cd本身正在工作&#xff0c;所以可执行文件应该在那里&#xff0c;对吧&#xff1f;我还为cd运行了find&#xff0c;但没有显示可执行文件。那怎么实现呢&#x…

Xenix — 微软与UNIX的短暂爱恋

微软向外宣布Microsoft Xenix OS&#xff0c;一个为16位微处理器开发的可移植的操作系统。它是一个交互的&#xff0c;多用户多任务系统&#xff0c;可以运行在Intel 8086, Zilog Z8000, Motorola M68000以及DEC公司的PDP-11系统计算机上。所有微软已经开发的系统软件&#xff…

optionals_Java Optionals获得更具表现力的代码

optionals我们中任何使用允许空引用的语言进行编程的人&#xff0c;都将在尝试取消引用一个引用时经历过。 无论是导致segfault还是NullPointerException&#xff0c;它始终是一个错误。 托尼霍尔将其描述为他十亿美元的错误 。 当函数向客户端的开发人员未预料到的客户端返回空…

python排序sorted_sorted排序的两个方法 - Python

在给列表排序时&#xff0c;sorted非常好用&#xff0c;语法如下&#xff1a; sorted(iterable[, cmp[,key[,reverse]]]) 简单列表排序&#xff0c;很容易完成&#xff0c;sorted(list)返回的对象就是列表结果&#xff0c;但是遇到列表中嵌套元组时&#xff0c;需要使用特殊的方…