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
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...
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.
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.
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.
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?
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) ); ```
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) );
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.
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).
A pretty little neat caveat with comparators, love it! Great video as always, guys.
We agree!
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
Sorry! 😆
I know the feeling 😂
Great video! Good Job!
wonderful video, thank you ☺️
Good advice... though subtracting two non-negative integers can't actually overflow, so the String.length() example would actually work.
First viewer!
Thank you Java Developers!
Thank you.
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...
Sorry that's a typo. sorted() is on Stream, sort() is on List since Java 8.
Could please add more about why comparator has equals methods but comparable not,
Or
How comparator use the equals methods while comparing objects ??
Hope to see a video about jep 445 as soon as possible. "make Java simpler for beginners"
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.
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.
Thanks
Is it just me, or does that 'coffee sip' look more rehearsed than an actor's lines in a Hollywood blockbuster? 🎬☕ #FakeSipper
Sir Paumard never fails to impress..Thank you so much for such an informative video.
Sir any plans for any new upcoming course ?
Thank you!
That is not the pace to drink coffee, how about drinking little bit wine, which perfectly fit the vibe. 🍷
Important note: BigDecimal#compareTo == 0 and BigDecimal#equals are inconsistent. Use compareTo if you need the "math" equality check
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.
@@TheBigLou13 actually you may need both methods: implement comparable to sort or similar usage
Tnx
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?
I wish no music background. Hard to listen 😅
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)
);
```
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)
);
Yikes, that reversed() one is nasty.
Bad design forcing the objects inside the set to be immutable
"var points;" -- FU
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.
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).