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

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
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
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: