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
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
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
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
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
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
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

Other popular topics Top

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
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25883 760
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
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: