【问题标题】:Remove duplicate objects from arraylist and consolidate data with unique object从 arraylist 中删除重复对象并使用唯一对象合并数据
【发布时间】:2023-03-03 12:34:01
【问题描述】:

请帮我解决以下任务,如果您有任何问题,请告诉我。

我在 departmentList 对象中有数据。在 departmentList 中有 3 个部门对象。对象 1 有 2 条员工记录。对象 3 是单独的 dept 对象,但具有相同的 deptid。

(Deptobject DEPTID DEPTNAME Location EmpId EmpName Empsal)

1 1 ABC NY 1 大卫 $5000

1 1 ABC NY 2 山姆 $6000

2 2 PQR NC 4 布赖恩 $5500

3 1 ABC NY 5 Glen $7000

我需要将数据整合到一个对象中(在唯一的 deptid 下)。

上述示例数据的预期输出:- (departmentList 包含 2 个对象,object1 (depid:1) 应该在 1 个employeelist 对象下包含 3 个employee 对象)

(Deptobject DEPTID DEPTNAME Location EmpId EmpName Empsal)

1 1 ABC NY 1 大卫 $5000

1 1 ABC NY 2 山姆 $6000

2 2 PQR NC 4 布赖恩 $5500

1 1 ABC NY 5 Glen $7000

List<Department> departmentsList = new ArrayList<Department>(); 

public Department
 {
  int deptId;
  String deptName;
  String deptLocation;
  List<Employee> employeeList=new ArrayList<Employee>();

 getterxxx();
 setters();
private void addEmployee(Employee e)
{
 this.employeeList.add(e);
 }

}

 public class  Employee
 {
  int empId;
  String empName;
  int sal;
 }

提前致谢!

【问题讨论】:

  • 恐怕我不明白你的意思,请你解释一下好吗?
  • 你的问题不清楚。如果您不想拥有同一对象的两个实例,您可以使用 Set,但我不确定您在寻找什么。
  • 基本上,我们从另一个系统/模块获取大的 ArrayList 对象。所以,我们不能更改其他系统/模块的代码。给定的(源)arraylist 有多个部门的arraylist。多个部门数组列表可以具有相同的部门 ID。每个部门数组列表都有多个员工数组列表。因此,我们需要通过删除重复的部门数组列表来处理那个大数组列表,并且在删除重复的部门数组列表之前将他们的员工数组列表对象移动到一个部门对象(基于 deptId)。所以最终列表应该包含唯一的 deptid objs

标签: java


【解决方案1】:

已经解决了。

    //Source arrayList
    ArrayList<Department> originalDepartmentsList=externalSys.getDepartmentsList();

    //for duplicate Department
    ArrayList<Department> duplicateDeptList = new ArrayList<Department>();

    //for unique Department
    HashMap<String,Department> uniqueDepts = new HashMap<String,Department>();

    //For duplicate identification and create a seperate list purpose
    Set<String> deptIdsList = new HashSet<String>();    

    Iterator originalIte=originalDepartmentsList.iterator();
    System.out.println("size of Department objs count in originalDepartmentsList.."+originalDepartmentsList.size());

    while(originalIte.hasNext())
    {
        Department deptObj=(Department) originalIte.next();
        String deptId=deptObj.getDeptId();
        if(!deptIdsList.add(deptId))
        {
            duplicateDeptList.add(deptObj);
            System.out.println("Duplicate SId is."+deptId);
        }
        else
        {
            System.out.println("deptId "+deptId+" is added to the uniques map.");
            uniqueDepts.put(deptId,deptObj);
        }       

    }

    System.out.println("duplicateDeptList size."+duplicateDeptList.size());
    System.out.println("uniqueDepts size."+uniqueDepts.size());
    System.out.println("********************************");

    //Adding Employee objects to the unique Department list.
    Iterator dupListIte=duplicateDeptList.iterator();
    while(dupListIte.hasNext())
    {
        Department deptObj=(Department) dupListIte.next();
        String deptId=deptObj.getDeptId();
        System.out.println("Duplicate DeptId."+deptId);

        ArrayList<Employee> employeeList= deptObj.getEmployeeList();

       if(employeeList!=null && employeeList.size()>0){

         Iterator empIte=employeeList.iterator();
        while(empIte.hasNext())
        {
            System.out.println("Employee list size before: for DeptId "+deptId+" .."+uniqueDepts.get(deptId).getEmployeeList().size());
            Employee empObj=(Employee) empIte.next();
            uniqueDepts.get(deptId).addToEmployeeList(empObj);
            System.out.println("Employee list size after: for DeptId "+deptId+" .."+uniqueDepts.get(deptId).getEmployeeList().size());
        }
}

}
List finalDeptList=new ArrayList(uniqueDepts.values());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2021-03-11
    相关资源
    最近更新 更多