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 Pragprog topics

The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New

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

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New

Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New

Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
New

When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New

When running tox for the first time, I got the following error:
ERROR: InterpreterNotFound: python3.10
I realised that I was running ...
New

Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New

Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Other popular topics

What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

I’m thinking of buying a monitor that I can rotate to use as a vertical monitor?
Also, I want to know if someone is using it for program...
New

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

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

Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New

The File System Access API with Origin Private File System.
WebKit supports new API that makes it possible for web apps to create, open,...
New

Author Spotlight: David Bryant Copeland (@davetron5000)
We’re so happy to bring you another Author Spotlight, a series where we sit d...
New

A Ruby-Centric Chat with Noel Rappin @noelrappin
Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /flutter
- /preact
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm