Source:
<code>private boolean populateClientStatesMap(final Map<UUID, ClientState> clientStates, final Map<UUID, ClientMetadata> clientMetadataMap, final Map<TopicPartition, TaskId> taskForPartition, final Map<TaskId, Set<TopicPartition>> changelogsByStatefulTask, final Set<String> newlyCreatedChangelogs) { boolean fetchEndOffsetsSuccessful; Map<TaskId, Long> allTaskEndOffsetSums; try { <START> final Collection<TopicPartition> allExistingChangelogPartitions = <END> changelogsByStatefulTask.values().stream() .flatMap(Collection::stream) .collect(Collectors.toList()); final Collection<TopicPartition> allNewChangelogPartitions = new ArrayList<>(allExistingChangelogPartitions); allExistingChangelogPartitions.removeIf(partition -> newlyCreatedChangelogs.contains(partition.topic())); allNewChangelogPartitions.removeAll(allExistingChangelogPartitions); final Map<TopicPartition, ListOffsetsResultInfo> endOffsets = fetchEndOffsets(allExistingChangelogPartitions, adminClient, Duration.ofMillis(adminClientTimeout)); allTaskEndOffsetSums = computeEndOffsetSumsByTask(endOffsets, changelogsByStatefulTask, allNewChangelogPartitions); fetchEndOffsetsSuccessful = true; } catch (final StreamsException e) { allTaskEndOffsetSums = null; fetchEndOffsetsSuccessful = false; } for (final Map.Entry<UUID, ClientMetadata> entry : clientMetadataMap.entrySet()) { final UUID uuid = entry.getKey(); final ClientState state = entry.getValue().state; state.initializePrevTasks(taskForPartition); if (fetchEndOffsetsSuccessful) { state.computeTaskLags(uuid, allTaskEndOffsetSums); } clientStates.put(uuid, state); } return fetchEndOffsetsSuccessful; }</code><technical_language>I immutability. call this allExpetcedChangelogPartitions introduce allExistingChangelogPartitions = allExpetcedChangelogPartitions - newlyCreatedChangelogs</technical_language>

Target:
private boolean populateClientStatesMap(final Map<UUID, ClientState> clientStates, final Map<UUID, ClientMetadata> clientMetadataMap, final Map<TopicPartition, TaskId> taskForPartition, final Map<TaskId, Set<TopicPartition>> changelogsByStatefulTask, final Set<String> newlyCreatedChangelogs) { boolean fetchEndOffsetsSuccessful; Map<TaskId, Long> allTaskEndOffsetSums; try { final Collection<TopicPartition> allChangelogPartitions = changelogsByStatefulTask.values().stream() .flatMap(Collection::stream) .collect(Collectors.toList()); final Collection<TopicPartition> allPreexistingChangelogPartitions = new ArrayList<>(allChangelogPartitions); allPreexistingChangelogPartitions.removeIf(partition -> newlyCreatedChangelogs.contains(partition.topic())); final Collection<TopicPartition> allNewlyCreatedChangelogPartitions = new ArrayList<>(allChangelogPartitions); allNewlyCreatedChangelogPartitions.removeAll(allPreexistingChangelogPartitions); final Map<TopicPartition, ListOffsetsResultInfo> endOffsets = fetchEndOffsets(allPreexistingChangelogPartitions, adminClient, Duration.ofMillis(adminClientTimeout)); allTaskEndOffsetSums = computeEndOffsetSumsByTask(endOffsets, changelogsByStatefulTask, allNewlyCreatedChangelogPartitions); fetchEndOffsetsSuccessful = true; } catch (final StreamsException e) { allTaskEndOffsetSums = null; fetchEndOffsetsSuccessful = false; } for (final Map.Entry<UUID, ClientMetadata> entry : clientMetadataMap.entrySet()) { final UUID uuid = entry.getKey(); final ClientState state = entry.getValue().state; state.initializePrevTasks(taskForPartition); if (fetchEndOffsetsSuccessful) { state.computeTaskLags(uuid, allTaskEndOffsetSums); } clientStates.put(uuid, state); } return fetchEndOffsetsSuccessful; }