Source:
<code>public Map<Address, Response> invokeRemotely(Map<Address, ReplicableCommand> rpcCommands, ResponseMode mode, long timeout, ResponseFilter responseFilter, DeliverOrder deliverOrder, boolean anycast) throws Exception { if (rpcCommands == null || rpcCommands.isEmpty()) { log.trace("Destination list is empty: no need to send message"); return Collections.emptyMap(); } if (trace) log.tracef("commands=%s, mode=%s, timeout=%s", rpcCommands, mode, timeout); boolean ignoreLeavers = mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE; CompletableFuture<Rsp<Response>>[] futures = new SingleResponseFuture[rpcCommands.size()]; int i = 0; for (Map.Entry<Address, ReplicableCommand> entry : rpcCommands.entrySet()) { org.jgroups.Address recipient = toJGroupsAddress(entry.getKey()); ReplicableCommand command = entry.getValue(); SingleResponseFuture future = dispatcher.invokeRemoteCommand(recipient, command, toJGroupsMode(mode), timeout, deliverOrder); futures[i] = future; i++; } if (mode.isAsynchronous()) return Collections.emptyMap(); CompletableFuture<Void> bigFuture = CompletableFuture.allOf(futures); CompletableFutures.await(bigFuture); List<Rsp<Response>> rsps = new ArrayList<>(futures.length); for (CompletableFuture<Rsp<Response>> future : futures) { rsps.add(future.get()); } Map<Address, Response> retval = new HashMap<>(CollectionFactory.computeCapacity(rsps.size())); boolean hasResponses = false; Iterator<Map.Entry<Address, ReplicableCommand>> rpcCommandsIterator = rpcCommands.entrySet().iterator(); <START> for (Rsp<Response> rsp : rsps) { <END> Address sender = rpcCommandsIterator.next().getKey(); Response response = checkRsp(rsp, sender, ignoreTimeout(responseFilter), ignoreLeavers); if (response != null) { retval.put(sender, response); hasResponses = true; } } if (!hasResponses) { throw new TimeoutException("Timed out waiting for valid responses!"); } return retval; }</code><technical_language>rsps List redundant, futures array avoid additional allocation copy</technical_language>

Target:
public Map<Address, Response> invokeRemotely(Map<Address, ReplicableCommand> rpcCommands, ResponseMode mode, long timeout, ResponseFilter responseFilter, DeliverOrder deliverOrder, boolean anycast) throws Exception { if (rpcCommands == null || rpcCommands.isEmpty()) { log.trace("Destination list is empty: no need to send message"); return Collections.emptyMap(); } if (trace) log.tracef("commands=%s, mode=%s, timeout=%s", rpcCommands, mode, timeout); boolean ignoreLeavers = mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE; CompletableFuture<Rsp<Response>>[] futures = new SingleResponseFuture[rpcCommands.size()]; int i = 0; for (Map.Entry<Address, ReplicableCommand> entry : rpcCommands.entrySet()) { org.jgroups.Address recipient = toJGroupsAddress(entry.getKey()); ReplicableCommand command = entry.getValue(); SingleResponseFuture future = dispatcher.invokeRemoteCommand(recipient, command, toJGroupsMode(mode), timeout, deliverOrder); futures[i] = future; i++; } if (mode.isAsynchronous()) return Collections.emptyMap(); CompletableFuture<Void> bigFuture = CompletableFuture.allOf(futures); CompletableFutures.await(bigFuture); Map<Address, Response> retval = new HashMap<>(CollectionFactory.computeCapacity(futures.length)); boolean hasResponses = false; i = 0; for (Map.Entry<Address, ReplicableCommand> addressReplicableCommandEntry : rpcCommands.entrySet()) { Address sender = addressReplicableCommandEntry.getKey(); Rsp<Response> rsp = futures[i].get(); Response response = checkRsp(rsp, sender, ignoreTimeout(responseFilter), ignoreLeavers); if (response != null) { retval.put(sender, response); hasResponses = true; } i++; } if (!hasResponses) { throw new TimeoutException("Timed out waiting for valid responses!"); } return retval; }