/** * Returns an instance of {@link PointShifts} built from the data in this builder. * * @return an instance of {@link PointShifts} built from the data in this builder */ public PointShifts build() { // This finds the scenario count by finding the maximum index and adding 1. // If OptionalInt had map() it could be written more sensibly as: ...max().map(i -> i + 1).orElse(0) // but it doesn't, hence using -1 and adding 1 to it for the case of zero scenarios int scenarioCount = shifts.keySet().stream() .mapToInt(Pair::getFirst) .max() .orElse(-1) + 1; List<Object> nodeIdentifiers = shifts.keySet().stream() .map(Pair::getSecond) .distinct() // Use distinct to preserve order. Collecting to a set wouldn't preserve it .collect(toImmutableList()); DoubleMatrix shiftMatrix = DoubleMatrix.of(scenarioCount, nodeIdentifiers.size(), (r, c) -> shiftValue(r, nodeIdentifiers.get(c))); return new PointShifts(shiftType, shiftMatrix, nodeIdentifiers); }