private void addOperations(MultiValueMap<OperationKey, O> indexed, EndpointId id, Object target, boolean replaceLast) { Set<OperationKey> replacedLast = new HashSet<>(); Collection<O> operations = this.operationsFactory.createOperations(id, target); for (O operation : operations) { OperationKey key = createOperationKey(operation); O last = getLast(indexed.get(key)); if (replaceLast && replacedLast.add(key) && last != null) { indexed.get(key).remove(last); } indexed.add(key, operation); } }