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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
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
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
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
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: