Write Efficient Bug-free and Simple Comparators in Java - JEP Café #17

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ม.ค. 2025

ความคิดเห็น • 34

  • @pavloskobnikov4298
    @pavloskobnikov4298 ปีที่แล้ว +20

    A pretty little neat caveat with comparators, love it! Great video as always, guys.

    • @java
      @java  ปีที่แล้ว +3

      We agree!

  • @robertmayer538
    @robertmayer538 ปีที่แล้ว +34

    Had to stop mid-video and change all int comparators from subtractions to Integer.compare() in my little hobby project. But I only do Java since 2000 or so, I'm still learning lmao

  •  ปีที่แล้ว +3

    Great video! Good Job!

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 ปีที่แล้ว

    wonderful video, thank you ☺️

  • @prdoyle
    @prdoyle ปีที่แล้ว +4

    Good advice... though subtracting two non-negative integers can't actually overflow, so the String.length() example would actually work.

  • @JavaSoftwareDeveloper
    @JavaSoftwareDeveloper ปีที่แล้ว +3

    First viewer!
    Thank you Java Developers!

  • @MrVitalirapalis
    @MrVitalirapalis ปีที่แล้ว +2

    Thank you.

  • @hausi78
    @hausi78 ปีที่แล้ว +1

    I'm confused. Around 2:50: expected would be "five", "four", "one", "three", "two". Or am I missing something? And around 2:26: what is var sortedStrings = strings.sorted(null); Is this Java 20? I know that strings.sort(null) would sort the list, but don't know of a *sorted(null)* possibility returning a sorted list...

    • @JosePaumard
      @JosePaumard ปีที่แล้ว +1

      Sorry that's a typo. sorted() is on Stream, sort() is on List since Java 8.

  • @anilpal4338
    @anilpal4338 ปีที่แล้ว +1

    Could please add more about why comparator has equals methods but comparable not,
    Or
    How comparator use the equals methods while comparing objects ??

  • @saarminator3343
    @saarminator3343 ปีที่แล้ว

    Hope to see a video about jep 445 as soon as possible. "make Java simpler for beginners"

  • @Speiger
    @Speiger ปีที่แล้ว

    One solution would to be just fix the difference function at least with integers/longs you could do Integer.signum(a1 - b1) where the output gets converted from difference to -1/0/1
    And that doesn't even have if statements :) since the Signum function only uses clever bitshifting.

  • @enzoscardamaglia9565
    @enzoscardamaglia9565 ปีที่แล้ว

    Good day and thank you for the JEP series.
    My question: what are the advantages of using
    intList.stream().sorted( ... ).toList();
    over
    Collections.sort(intList., ... )?
    Is it immutability (and maybe readability)?
    Thank you in advance.

  • @AjayKumar-fd9mv
    @AjayKumar-fd9mv ปีที่แล้ว +2

    Thanks

  • @ekadet
    @ekadet ปีที่แล้ว +1

    Is it just me, or does that 'coffee sip' look more rehearsed than an actor's lines in a Hollywood blockbuster? 🎬☕ #FakeSipper

  • @MakeItStik
    @MakeItStik ปีที่แล้ว +4

    Sir Paumard never fails to impress..Thank you so much for such an informative video.
    Sir any plans for any new upcoming course ?

  • @motionattached
    @motionattached 6 หลายเดือนก่อน

    That is not the pace to drink coffee, how about drinking little bit wine, which perfectly fit the vibe. 🍷

  • @madskaddie
    @madskaddie ปีที่แล้ว

    Important note: BigDecimal#compareTo == 0 and BigDecimal#equals are inconsistent. Use compareTo if you need the "math" equality check

    • @TheBigLou13
      @TheBigLou13 ปีที่แล้ว

      If both would do the same you wouldn't need two methods. _equals_ just checks if the variables you're comparing are referencing to the same thing. _compare_ checks their values.

    • @madskaddie
      @madskaddie ปีที่แล้ว

      @@TheBigLou13 actually you may need both methods: implement comparable to sort or similar usage

  • @jahpreacherman
    @jahpreacherman ปีที่แล้ว +2

    Tnx

  • @gavlosq846
    @gavlosq846 ปีที่แล้ว

    Sorry, but I don't understand one thing. Why in the example with a comparator for a Set with 10 elements(and mutable object) everything is ok? Is it due to the fact that a red-black tree is not created, but a regular LinkedList?

  • @seeboonsoo
    @seeboonsoo 8 หลายเดือนก่อน +1

    I wish no music background. Hard to listen 😅

  • @tarphuer
    @tarphuer ปีที่แล้ว +2

    There are many pits with Comparator. for example:
    This does not compile:
    ```
    record Person(String firstName, String lastName) {
    }
    public static void main(String[] args) {
    List ps = new ArrayList();
    ps.sort(
    Comparator.comparing(a -> a.firstName)
    .thenComparing(a -> a.lastName)
    );
    }
    ```,
    the way to fix this is
    ```
    record Person(String firstName, String lastName) {
    }
    public static void main(String[] args) {
    List ps = new ArrayList();
    ps.sort(
    Comparator.comparing((Person a) -> a.firstName)
    .thenComparing(a -> a.lastName)
    );
    }
    ```.
    Another thing is, this does not reverse the comparison on the first name and keep natural order for lastName, but the whole thing
    ```
    ps.sort(
    Comparator.comparing((Person a) -> a.firstName)
    .reversed()
    .thenComparing(a -> a.lastName)
    );
    ```
    The right way to do this is:
    ```
    ps.sort(
    Comparator.comparing((Person a) -> a.firstName, Comparator.reverseOrder())
    .thenComparing(a -> a.lastName)
    );
    ```

    • @JosePaumard
      @JosePaumard ปีที่แล้ว +4

      Your fist "issue" is not a Comparator stuff but comes from the way the type system is working. A much more elegant way to fix it:
      List ps = new ArrayList();
      ps.sort(
      Comparator.comparing(Person::firstName)
      .thenComparing(Person::lastName)
      );

    • @prdoyle
      @prdoyle ปีที่แล้ว +1

      Yikes, that reversed() one is nasty.

  • @faceITnow
    @faceITnow ปีที่แล้ว

    Bad design forcing the objects inside the set to be immutable

  • @nicholas1460
    @nicholas1460 ปีที่แล้ว

    "var points;" -- FU

  • @Intusr
    @Intusr ปีที่แล้ว

    Is it still relevant to learn JAVA guys I am new to computer science with no programming experience I want to venture into the data science world. So if you guys can suggest me a plan that would be great thanks in advance.

    • @TheBigLou13
      @TheBigLou13 ปีที่แล้ว +1

      yes. But if you're just into data science (processing data) and not so much into developing something, then python is a widely used contender (scripting language).