
dtonhofer
Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 193 ff “Refactoring Data Grouping Operations”
As usual, but the original code to be refactored was just too evil and had to be fixed.
package chapter11;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DataGroupingOperationsTest {
interface Scores {
Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores);
}
public static class ScoresBefore implements Scores {
public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
final Map<Integer, Set<String>> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
final String name = entry.getKey();
final Integer score = entry.getValue();
if (!result.containsKey(score)) {
result.put(score, new HashSet<>());
}
result.get(score).add(name);
}
return result;
}
}
public static class ScoresAfter implements Scores {
public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
return scores.keySet().stream()
.collect(groupingBy(scores::get, toSet()));
}
}
private static void commonNamesForScoresTests(final Scores scores) {
assertAll(
() -> assertEquals(Map.of(), scores.namesForScores(Map.of())),
() -> assertEquals(
Map.of(1, Set.of("Jill")), scores.namesForScores(Map.of("Jill", 1))),
() -> assertEquals(
Map.of(1, Set.of("Jill"), 2, Set.of("Paul")),
scores.namesForScores(Map.of("Jill", 1, "Paul", 2))),
() -> assertEquals(
Map.of(1, Set.of("Jill", "Kate"), 2, Set.of("Paul")),
scores.namesForScores(Map.of("Jill", 1, "Paul", 2, "Kate", 1)))
);
}
@Test
void scoresBefore() {
commonNamesForScoresTests(new ScoresBefore());
}
@Test
void scoresAfter() {
commonNamesForScoresTests(new ScoresAfter());
}
}
Popular Pragmatic topics

Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list)
Typo:...
New

your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New

Hi @venkats,
It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New

The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New

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

This is as much a suggestion as a question, as a note for others.
Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New

I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New

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

On page 78 the following code appears:
<%= link_to ‘Destroy’, product,
class: ‘hover:underline’,
method: :delete,
data: { confirm...
New

@mfazio23
I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
Other popular topics

No chair. I have a standing desk.
This post was split into a dedicated thread from our thread about chairs :slight_smile:
New

Why, if your answer is yes?
New

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

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

Not sure if following fits exactly this thread, or if we should have a hobby thread…
For many years I’m designing and building model air...
New

“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco
Ever wonder how authoring books compares to writing articles?...
New

Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New

Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New

Hi folks,
I don’t know if I saw this here but, here’s a new programming language, called Roc
Reminds me a bit of Elm and thus Haskell. ...
New

Author Spotlight:
Tammy Coron
@Paradox927
Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /textmate
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim