This post is the continuation of Mapping composition in Java.

This time, I:

  1. Disallowed the null values as the range value.
  2. Simplified the toString method via using the streams.



package com.github.coderodde.mapping;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

 * This class implements a mapping from a domain set to a range set.
 * @param <D> the domain element type.
 * @param <R> the range element type.
 * @author Rodion "rodde" Efremov
 * @version 1.61 (Sep 14, 2023)
 * @since 1.6 (Sep 3, 2023)
public final class Mapping<D, R> {

    final Map<D, R> data = new HashMap<>();
    public void map(D domainValue, R rangeValue) {
                               "The range value is not allowed to be nul..");
        data.put(domainValue, rangeValue);
    public boolean isMapped(D domainValue) {
        return data.containsKey(
                        "The input domain value is null."));
    public R map(D domainValue) {
        return data.get(domainValue);
    public String toString() {
        return data.entrySet()
                   .collect(Collectors.joining(", ", "[", "]"));
    private static <D, R> 
        String convertMapEntryToString(Map.Entry<D, R> entry) {
        return new StringBuilder()
                    .append(" -> ")
    private void checkDomainValueNotYetMapped(D domainValue) {
        if (data.containsKey(
                Objects.requireNonNull(domainValue, "Domain value is null."))) {
            throw new DuplicateDomainValueException(
                    "Trying to map a domain value ["
                            + domainValue
                            + "] twice.");
    private void checkDomainValueIsMapped(D domainValue) {
        if (!data.containsKey(domainValue)) {
            throw new DomainValueIsNotMappedException(
                    "Domain value [" + domainValue + "] is not mapped.");

Critique request

As always, please tell me whatever comes to mind.

  • \$\begingroup\$ Why dont you use the adapter pattern? Could you fix spotbugs, PMD and checkstyle issues? Are you signing the jar? \$\endgroup\$
    – Grim
    Sep 17, 2023 at 8:17

1 Answer 1


Your Mapping class seems unnecessary, as your compose function shown in the previous post could just use Map types, which are already defined in a standard way within Java. all you're really doing is wrapping the methods of the inner data instance variable which is a map.

  • \$\begingroup\$ True. However, note that rolling my own wrapping class gives me — at the very least— control over the way a map is converted to a string. \$\endgroup\$
    – coderodde
    Sep 20, 2023 at 9:06
  • \$\begingroup\$ Yeah and that could be extracted out into a separate function that takes a map as input and then you wouldn’t have to convert existing maps into Mapping to use your new functionality \$\endgroup\$ Sep 21, 2023 at 14:38
  • \$\begingroup\$ I think it’d be cool to have compose be a variadic function \$\endgroup\$ Sep 21, 2023 at 14:42
  • \$\begingroup\$ Not quite sure how to write variadic functions. Wanna elaborate your answer? \$\endgroup\$
    – coderodde
    Sep 21, 2023 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.