You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按规则将Employee列表分配至4组?Java代码优化咨询

解决方案:员工跨原组分组实现

你的需求回顾

存在存储员工列表的Employee对象,每位员工拥有1-4的groupNumber;希望员工能在所有组别(含原组别)积累经验,新生成的每组中至少包含1名来自不同原组的员工(理想状态为每组4人分别来自不同原组)。现有代码仅实现随机排序,请问需如何修改以满足上述条件?

现有代码问题分析

先看你提供的代码(格式化后):

public class EmployeeTest { 
    public static void main(String[] args) { 
        Random random = new Random(); 
        int group_num = random.nextInt(30); 
        int max = 0; 
        if(max > 4) { 
            group_num = 1; 
            ArrayList<Employee> emps = new ArrayList(); 
            emps.add(new Employee("Tom", "Technology", 56, 34, 46, 23)); 
            emps.add(new Employee("Nick", "Technology", 65, 33, 21, 23)); 
            emps.add(new Employee("Jerry", "Technology", 56, 34, 46, 23)); 
            emps.add(new Employee("Janet", "Technology", 56, 34, 46, 23)); 
            emps.add(new Employee("Chitra", "Trading", 12, 54, 46, 23)); 
            emps.add(new Employee("Sally", "Trading", 16, 64, 46, 23)); 
            for(Employee e : emps) { 
                // Random random = new Random(); 
                // int group_num = random.nextInt(30); 
                // int max =0; 
                // if(max > 4) { 
                // group_num = 1; 
                // } 
                System.out.println("Employee | identifier: " + e.getUnicIdentifier() + ", department: " + e.getDepartment() + ", texh_exp: " + e.getTech_exp() + " , trade_exp: " + e.getTrade_exp() + " , op_exp: " + e.getOp_exp() + ", shared_exp: " + e.getShared_exp() + ", group_num: " + group_num); 
            } 
        } 
    } 
}

这里有几个关键问题:

  1. 逻辑死区:max初始值是0,if(max >4)永远不会触发,所以里面的员工初始化和循环代码根本不会执行。
  2. 无分组逻辑:当前代码只是生成一个随机组号,然后把所有员工都分到这个组里,完全没有实现跨原组分组的需求。
  3. 缺少原组属性:你的Employee对象没有体现groupNumber(原组号),这是实现需求的核心字段。

完整修改方案

第一步:完善Employee类

首先给Employee添加原组号属性和对应的get方法:

class Employee {
    private String name;
    private String department;
    private int tech_exp;
    private int trade_exp;
    private int op_exp;
    private int shared_exp;
    private int groupNumber; // 原组号,1-4
    private String unicIdentifier; // 唯一标识,这里用name简化处理

    public Employee(String name, String department, int tech_exp, int trade_exp, int op_exp, int shared_exp, int groupNumber) {
        this.name = name;
        this.department = department;
        this.tech_exp = tech_exp;
        this.trade_exp = trade_exp;
        this.op_exp = op_exp;
        this.shared_exp = shared_exp;
        this.groupNumber = groupNumber;
        this.unicIdentifier = name; // 实际项目中建议用UUID等唯一值
    }

    // Getter方法
    public String getUnicIdentifier() { return unicIdentifier; }
    public String getDepartment() { return department; }
    public int getTech_exp() { return tech_exp; }
    public int getTrade_exp() { return trade_exp; }
    public int getOp_exp() { return op_exp; }
    public int getShared_exp() { return shared_exp; }
    public int getGroupNumber() { return groupNumber; }
}

第二步:实现跨原组分组逻辑

修改EmployeeTest类,实现轮询分配员工到新组,确保每组至少包含不同原组的员工:

import java.util.*;

public class EmployeeTest {
    public static void main(String[] args) {
        // 初始化员工列表,给每个员工指定原组号(1-4)
        List<Employee> emps = new ArrayList<>();
        emps.add(new Employee("Tom", "Technology", 56, 34, 46, 23, 1));
        emps.add(new Employee("Nick", "Technology", 65, 33, 21, 23, 1));
        emps.add(new Employee("Jerry", "Technology", 56, 34, 46, 23, 2));
        emps.add(new Employee("Janet", "Technology", 56, 34, 46, 23, 2));
        emps.add(new Employee("Chitra", "Trading", 12, 54, 46, 23, 3));
        emps.add(new Employee("Sally", "Trading", 16, 64, 46, 23, 4));
        emps.add(new Employee("Bob", "HR", 22, 33, 44, 55, 3));
        emps.add(new Employee("Alice", "HR", 11, 22, 33, 44, 4));

        // 按原组号分组,用队列方便依次取人
        Map<Integer, Queue<Employee>> originalGroups = new HashMap<>();
        for (int i = 1; i <= 4; i++) {
            originalGroups.put(i, new LinkedList<>());
        }
        for (Employee emp : emps) {
            originalGroups.get(emp.getGroupNumber()).add(emp);
        }

        // 生成新组:轮询从每个原组取人,理想每组4人各来自不同原组
        List<List<Employee>> newGroups = new ArrayList<>();
        boolean hasEmployeesLeft = true;

        while (hasEmployeesLeft) {
            List<Employee> newGroup = new ArrayList<>();
            // 从4个原组各取一个员工(如果有的话)
            for (int groupId = 1; groupId <= 4; groupId++) {
                Queue<Employee> groupQueue = originalGroups.get(groupId);
                if (!groupQueue.isEmpty()) {
                    newGroup.add(groupQueue.poll());
                }
            }
            // 只有当新组有员工时才加入列表
            if (!newGroup.isEmpty()) {
                newGroups.add(newGroup);
            } else {
                hasEmployeesLeft = false;
            }
        }

        // 输出所有新组的信息
        for (int i = 0; i < newGroups.size(); i++) {
            int newGroupNum = i + 1;
            System.out.println("=== 新组 " + newGroupNum + " ===");
            for (Employee e : newGroups.get(i)) {
                System.out.printf(
                    "Employee | identifier: %s, department: %s, tech_exp: %d, trade_exp: %d, op_exp: %d, shared_exp: %d, 原组号: %d\n",
                    e.getUnicIdentifier(), e.getDepartment(), e.getTech_exp(), e.getTrade_exp(), e.getOp_exp(), e.getShared_exp(), e.getGroupNumber()
                );
            }
        }
    }
}

代码逻辑说明

  1. 按原组分组:用Map<Integer, Queue<Employee>>把每个原组的员工存起来,队列结构能方便我们依次取出员工,避免重复分配。
  2. 轮询分配:每次循环从4个原组各取一个员工组成新组,这样每个新组自然会包含不同原组的员工,完美符合你要的理想状态。
  3. 处理剩余员工:如果某个原组还有员工没分配完,会在后续循环中被加入到新组,确保这些剩余员工不会单独组成全是同一原组的组(除非只剩一个原组的员工,这种情况你可以根据需求调整,比如合并到已有组)。
  4. 修正原有错误:移除了无效的max判断,正确初始化员工列表,完善了Employee类的核心属性。

内容的提问来源于stack exchange,提问作者Links

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:15:33