private List<TaskGroup> getOrderedContainers(Map<TaskName, String> taskToContainerId) {
    LOG.debug("Got task to container map: {}", taskToContainerId);

    // Group tasks by container Id
    Map<String, List<String>> containerIdToTaskNames = new HashMap<>();
    for (Map.Entry<TaskName, String> entry : taskToContainerId.entrySet()) {
      String taskName = entry.getKey().getTaskName();
      String containerId = entry.getValue();
      List<String> taskNames = containerIdToTaskNames.computeIfAbsent(containerId, k -> new ArrayList<>());
      taskNames.add(taskName);
    }

    // Build container tasks
    List<TaskGroup> containerTasks = new ArrayList<>(containerIdToTaskNames.size());
    for (int i = 0; i < containerIdToTaskNames.size(); i++) {
      if (containerIdToTaskNames.get(String.valueOf(i)) == null) throw new IllegalStateException("Task mapping is missing container: " + i);
      containerTasks.add(new TaskGroup(String.valueOf(i), containerIdToTaskNames.get(String.valueOf(i))));
    }

    return containerTasks;
  }
Converts the task->containerId map to an ordered list of {@link TaskGroup} instances.

@param taskToContainerId a map from each task name to the containerId to which it is assigned.
@return                  a list of TaskGroups ordered ascending by containerId.
samza-core/src/main/java/org/apache/samza/container/grouper/task/GroupByContainerCount.java