FxMatrixBuilder merge(FxMatrixBuilder other) { // Find the common currencies Optional<Currency> common = currencies.keySet() .stream() .filter(other.currencies::containsKey) .findFirst(); Currency commonCurrency = common.orElseThrow(() -> new IllegalArgumentException( "There are no currencies in common between " + currencies.keySet() + " and " + other.currencies.keySet())); // Add in all currencies that we don't already have MapStream.of(other.currencies) .filterKeys(ccy -> !ccy.equals(commonCurrency) && !currencies.containsKey(ccy)) .forEach((ccy, idx) -> addCurrencyPair(commonCurrency, ccy, other.getRate(commonCurrency, ccy))); return this; }