enjoy = enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null);中怎么吧“{ if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } ”提为变量

时间: 2024-02-10 17:11:57 浏览: 22
可以将这段代码提取为一个lambda表达式或者一个匿名内部类,然后将其作为Comparator.comparing()方法的参数。示例代码如下: ```java Comparator<GoodsEnjoy> comparator = Comparator.comparing( GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } } ); GoodsEnjoy enjoy = enjoys.stream().sorted(comparator).findFirst().orElse(null); ``` 这样可以让代码更加清晰易懂,也方便后续的维护和修改。
相关问题

enjoy = enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null);和 excellent = excellents.stream().sorted(Comparator.comparing(GoodsExcellent::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null);这简化一下

可以将这两段代码提取为一个通用的方法,然后复用该方法。示例代码如下: ```java private static <T, R extends Comparable<? super R>> T findFirstSorted(List<T> list, Function<T, R> keyExtractor) { Comparator<R> comparator = (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } }; return list.stream().sorted(Comparator.comparing(keyExtractor, comparator)).findFirst().orElse(null); } GoodsEnjoy enjoy = findFirstSorted(enjoys, GoodsEnjoy::getGroundingState); GoodsExcellent excellent = findFirstSorted(excellents, GoodsExcellent::getGroundingState); ``` 这样可以让代码更加简洁,也方便后续的维护和修改。

enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null);将Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })提为变量调用怎么改

你可以将Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })提为一个变量,例如: Comparator<GoodsEnjoy> comparator = Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } }); 然后可以在排序中使用这个变量,例如: enjoys.stream().sorted(comparator).findFirst().orElse(null); 这样就能达到将Comparator提为变量的目的了。

相关推荐

switch (goodsItem.getProductType()) { case 1://爆品 GoodsExplosive explosive = null; List<GoodsExplosive> explosives = new GoodsExplosive().selectList(new LambdaUpdateWrapper<GoodsExplosive>() .eq(GoodsExplosive::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExplosive::getId)); if (explosives.size() > 1) { explosive = explosives.stream().sorted(Comparator.comparing(GoodsExplosive::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { explosive = explosives.size() == 1 ? explosives.get(0) : explosive; } map.put("productData", explosive); break; case 2://优品 GoodsExcellent excellent = null; List<GoodsExcellent> excellents = new GoodsExcellent().selectList(new LambdaUpdateWrapper<GoodsExcellent>() .eq(GoodsExcellent::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsExcellent::getId)); if (excellents.size() > 1) { excellent = excellents.stream().sorted(Comparator.comparing(GoodsExcellent::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1; } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { excellent = excellents.size() == 1 ? excellents.get(0) : excellent; } map.put("productData", excellent); break; case 3://现货 GoodsEnjoy enjoy = null; List<GoodsEnjoy> enjoys = new GoodsEnjoy().selectList(new LambdaUpdateWrapper<GoodsEnjoy>() .eq(GoodsEnjoy::getGoodsItemId, goodsItem.getId()) .orderByDesc(GoodsEnjoy::getId)); if (enjoys.size() > 1) { enjoy = enjoys.stream().sorted(Comparator.comparing(GoodsEnjoy::getGroundingState, (s1, s2) -> { if (s1.equals(s2)) { return 0; } else if (s1 == 1 || s2 == 5) { return -1; } else if (s1 == 5 || s2 == 1) { return 1 } else if (s1 == 2) { return -1; } else if (s2 == 2) { return 1; } else if (s1 == 0) { return -1; } else { return 1; } })).findFirst().orElse(null); } else { enjoy = enjoys.size() == 1 ? enjoys.get(0) : enjoy; } map.put("productData", enjoy); break; default: throw new IllegalStateException("Unexpected value: " + goodsItem.getProductType()); }怎么优化

Dog Card is a card game. In the game, there are a total of 2n cards in the deck, each card has a value, and the values of these 2n cards form a permutation of 1 ~ 2n. There is a skill that works as follows: 1. Draw a card from the top of the deck. 2. If the deck is empty, then skip to step 3, otherwise you guess whether the card on the top of the deck has a higher value than your last drawn card and draw a card from the top of the deck. If your guess is correct, then repeat this step, otherwise skip to step 3. 3. End this process. Nana enjoys playing this game, although she may not be skilled at it. Therefore, her guessing strategy when using this skill is simple: if the value of the last drawn card is less than or equal to n, then she guesses that the next oard's valve is higher, ther wse, she guedses thet the next card's vaue s lomler she wârns tb dmokt tor anfafrhlm decks of cards (Obviously, there are (2n)! cases), how many cards she can draw in total if she uses the skill only once in each case. Since this number can be very large,please provide the answer modulo a given value.Dog Card is a card game. In the game, there are a total of 2n cards in the deck, each card has a value, and the values of these 2n cards form a permutation of 1 ~ 2n. There is a skill that works as follows: 1. Draw a card from the top of the deck. 2. If the deck is empty, then skip to step 3, otherwise you guess whether the card on the top of the deck has a higher value than your last drawn card and draw a card from the top of the deck. If your guess is correct, then repeat this step, otherwise skip to step 3. 3. End this process. Nana enjoys playing this game, although she may not be skilled at it. Therefore, her guessing strategy when using this skill is simple: if the value of the last drawn card is less than or equal to n, then she guesses that the next oard's valve is higher, ther wse, she guedses thet the next card's vaue s lomler she wârns tb dmokt tor anfafrhlm decks of cards (Obviously, there are (2n)! cases), how many cards she can draw in total if she uses the skill only once in each case. Since this number can be very large,please provide the answer modulo a given value.给出c++代码及中文解释

最新推荐

recommend-type

scrapy练习 获取喜欢的书籍

主要是根据网上大神做的 项目一 https://zhuanlan.zhihu.com/p/687522335
recommend-type

基于PyTorch的Embedding和LSTM的自动写诗实验.zip

基于PyTorch的Embedding和LSTM的自动写诗实验LSTM (Long Short-Term Memory) 是一种特殊的循环神经网络(RNN)架构,用于处理具有长期依赖关系的序列数据。传统的RNN在处理长序列时往往会遇到梯度消失或梯度爆炸的问题,导致无法有效地捕捉长期依赖。LSTM通过引入门控机制(Gating Mechanism)和记忆单元(Memory Cell)来克服这些问题。 以下是LSTM的基本结构和主要组件: 记忆单元(Memory Cell):记忆单元是LSTM的核心,用于存储长期信息。它像一个传送带一样,在整个链上运行,只有一些小的线性交互。信息很容易地在其上保持不变。 输入门(Input Gate):输入门决定了哪些新的信息会被加入到记忆单元中。它由当前时刻的输入和上一时刻的隐藏状态共同决定。 遗忘门(Forget Gate):遗忘门决定了哪些信息会从记忆单元中被丢弃或遗忘。它也由当前时刻的输入和上一时刻的隐藏状态共同决定。 输出门(Output Gate):输出门决定了哪些信息会从记忆单元中输出到当前时刻的隐藏状态中。同样地,它也由当前时刻的输入和上一时刻的隐藏状态共同决定。 LSTM的计算过程可以大致描述为: 通过遗忘门决定从记忆单元中丢弃哪些信息。 通过输入门决定哪些新的信息会被加入到记忆单元中。 更新记忆单元的状态。 通过输出门决定哪些信息会从记忆单元中输出到当前时刻的隐藏状态中。 由于LSTM能够有效地处理长期依赖关系,它在许多序列建模任务中都取得了很好的效果,如语音识别、文本生成、机器翻译、时序预测等。
recommend-type

基于Java的云计算平台设计源码

云计算平台设计源码:该项目基于Java开发,包含126个文件,主要使用Java、JavaScript、HTML和CSS语言。该项目是一个云计算平台,旨在为用户提供一个高效、灵活的云计算服务,包括资源管理、虚拟化技术、分布式存储等功能,以满足不同用户的计算和存储需求。
recommend-type

grpcio-1.41.0-cp38-cp38-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

grpcio-1.44.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。