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

Hi everyone!
There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New

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

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New

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

When I run the coverage example to report on missing lines, I get:
pytest --cov=cards --report=term-missing ch7
ERROR: usage: pytest [op...
New

Is the book’s epub format available to read on Google Play Books?
New

Hey there,
I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New

From page 13:
On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New
Other popular topics

Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
New

Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
New

New
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New

If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /textmate
- /sublime-text
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /django
- /manjaro
- /spring
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /neovim