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);这简化一下

时间: 2024-02-10 10:11:57 浏览: 119
可以将这两段代码提取为一个通用的方法,然后复用该方法。示例代码如下: ```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); ``` 这样可以让代码更加简洁,也方便后续的维护和修改。

相关推荐

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++代码及中文解释