From 8f8244e52047a56702257ed90c1250e1df8c499d Mon Sep 17 00:00:00 2001 From: Sjuice1 <76224197+Sjuice1@users.noreply.github.com> Date: Thu, 22 Feb 2024 13:17:30 +0200 Subject: [PATCH] Better answers Better answers --- .../src/main/java/com/bobocode/fp/CrazyLambdas.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java index 86812db5..6345d14f 100644 --- a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java +++ b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java @@ -265,7 +265,7 @@ public static BiFunction, String, IntUnaryOperator * @return a comparator instance */ public static > Comparator comparing(Function mapper) { - return (o1, o2) -> mapper.apply(o1).compareTo(mapper.apply(o2)); + return Comparator.comparing(mapper); } /** @@ -285,13 +285,7 @@ public static > Comparator comparing(Funct */ public static > Comparator thenComparing( Comparator comparator, Function mapper) { - return (o1, o2) -> { - var initialResult = comparator.compare(o1, o2); - if (initialResult != 0) { - return initialResult; - } - return mapper.apply(o1).compareTo(mapper.apply(o2)); - }; + return (o1,o2) -> comparator.compare(o1,o2) == 0 ? mapper.apply(o1).compareTo(mapper.apply(o2)) : comparator.compare(o1,o2); } /**