public static Set<ContainerModel> buildContainerModels(Set<TaskModel> taskModels, Collection<TaskGroup> taskGroups) {
    // Map task names to models
    Map<String, TaskModel> taskNameToModel = new HashMap<>();
    for (TaskModel model : taskModels) {
      taskNameToModel.put(model.getTaskName().getTaskName(), model);
    }

    // Build container models
    Set<ContainerModel> containerModels = new HashSet<>();
    for (TaskGroup container : taskGroups) {
      Map<TaskName, TaskModel> containerTaskModels = new HashMap<>();
      for (String taskName : container.taskNames) {
        TaskModel model = taskNameToModel.get(taskName);
        containerTaskModels.put(model.getTaskName(), model);
      }
      containerModels.add(new ContainerModel(container.containerId, containerTaskModels));
    }

    return Collections.unmodifiableSet(containerModels);
  }
Converts the {@link TaskGroup} list to a set of ContainerModel.

@param taskModels    the TaskModels to assign to the ContainerModels.
@param taskGroups    the TaskGroups defining how the tasks should be grouped.
@return              a set of ContainerModels.
samza-core/src/main/java/org/apache/samza/container/grouper/task/TaskGroup.java