dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: p. 55 "Compare.java" can be further simplified

On page 55, we see Compare.java

final Function<Person, String> byName = person -> person.getName();
people.stream()
   .sorted(comparing(byName));

However, this can be further simplified to:

people.stream()
   .sorted(comparing(Person::getName));

On the same page, more from Compare.java:

final Function<Person, Integer> byAge = person -> person.getAge();
final Function<Person, String> byTheirName = person -> person.getName();
printPeople("Sorted in ascending order by age, then name: ",
                people.stream()
                        .sorted(comparing(byAge).thenComparing(byTheirName))
                        .collect(toList());

which can be simplified similarly to:

printPeople("Sorted in ascending order by age, then name: ",
                people.stream()
                        sorted(comparing(Person::getAge).thenComparing(Person::getName))
                        .collect(toList());

but which would be even better by separating the list generation and output tasks:

List<Person> asc =
                people.stream()
                        .sorted(comparing(Person::getAge).thenComparing(Person::getName))
                        .collect(toList());
        printPeople("Sorted in ascending order by age, then name: ", asc);

…all according to the JavaDoc for Comparator:

P.S.

Would it be clearer to explicitly write the class from which comparing() comes instead of assuming it has been imported statically? Like this:

people.stream()
   .sorted(Comparator.comparing(Person::getName));

instaed of

people.stream()
   .sorted(comparing(Person::getName));

Note that the collect()call

people.stream()
   .sorted(comparing(byAge).thenComparing(byTheirName))
   .collect(toList()));

…also assume a statically imported toList(). On the other hand, compare/fpij/OlderThan20.java on page 58 is explicit:

List<Person> olderThan20 =
   people.stream()
      .filter(person -> person.getAge() > 20)
      .collect(Collectors.toList());

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1030 17300 381
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: