【问题标题】:How Can ArrayList Created on if Condition dynamically be accessed on Else Condition?如何在其他条件下动态访问在 if 条件上创建的 ArrayList?
【发布时间】:2017-11-10 08:45:50
【问题描述】:

我已经在列表中列出了数据,即姓名、性别、部门。我已经在列表中添加了不同部门的数据,如 QA、Java、Php。现在我必须将具有特定部门的数据放入哈希图中(例如,所有 QA 部门都应该动态地列在一个 ArrayList 或 List 中)。如果我添加新部门,那么它应该动态地列在新的 ArrayList 中,而不是通过硬编码。这是一个简单的逻辑,但我无法解决这个问题。

Getter 和 Setter

public class Employ {

    private String name;
    private int id;
    private String gender;
    private String dept;

    public Employ(String name,int id,String gender,String dept){
        this.name=name;
        this.id=id;
        this.gender=gender;
        this.dept=dept;

    }     

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the dept
     */
    public String getDept() {
        return dept;
    }

    /**
     * @param dept the dept to set
     */
    public void setDept(String dept) {
        this.dept = dept;
    }
}

主类

public class App {

    public static void main(String[] args) {
        String key="QA";
        String key2="java";
        HashMap<String ,List<Employ>> soft=new HashMap();

        List<Employ> employList=new ArrayList();
        employList.add(new Employ("ram",1, "male","qa"));
        employList.add(new Employ("tom",2, "male","qa"));
        employList.add(new Employ("arjun",3, "male","java"));
        employList.add(new Employ("hari",4, "male","java"));
        employList.add(new Employ("kumar",5, "male","qa"));
        employList.add(new Employ("sita",6, "female","java"));
        employList.add(new Employ("rajan",7, "female","qa"));
        employList.add(new Employ("rajal",8, "female","php"));

        for(Employ emp:employList){  
            if(!soft.containsKey(emp.getDept())){
                soft.put(emp.getDept(),new ArrayList());
            }else{
                // soft.put(emp.getDept(),add("qa"));
            }
        }
    }
}

如何做到这一点?

【问题讨论】:

  • f1soft.get(emp.getDept()).add("qa");
  • 您想按部门对员工进行分组,对吗? IE。有一个包含部门作为键的 Map,该部门的员工列表作为值?
  • @JBNIzet 是的,但是如果我添加到列表中,它应该也会自动添加到地图上

标签: java arraylist hashmap


【解决方案1】:

如果我理解正确,您想按部门对员工进行分组。

命令式的方法是

for (Employee employee: employeeList){  
    List<Employee> departmentList = f1soft.get(employee.getDepartment());
    if (departmentList == null) {
        departmentList = new ArrayList<>();
        f1soft.put(emp.getDepartment(), departmentList);
    }
    departmentList.add(employee);
}

或者,更简单:

for (Employee employee: employeeList){  
    f1soft.computeIfAbsent(department, d -> new ArrayList<Employee>()).add(employee);
}

但是使用流来做到这一点必须更简单:

Map<String, Employee> f1soft = 
    employeeList.stream()
                .collect(Collectors.groupingBy(Employee::getDepartment)); 

请注意,我选择重命名您的类型和属性以使所有内容更具可读性。确实没有充分的理由使用 Employ 代替 Employee,或 Dept 代替 Department。

【讨论】:

  • 为什么所有相​​同的 QA 没有一起列出
  • 未创建单独列表。
  • 是的,创建了单独的列表。您可能没有按照我的回答做。编辑您的问题并发布一个完整的最小示例。
  • 很简单,当 QA 密钥到来时,应该创建 arrayList,如果 next;y QA 来了,那么它应该添加到之前创建的 QA ArrayList 中
  • 是的,上面的代码就是这样做的。编辑您的问题并发布一个完整的最小示例来重现您的问题。
【解决方案2】:

如果Map 中存在键emp.getDept(),您可以使用f1soft.get(emp.getDept()) 获取对应的ArrayList 值,然后添加任何您想要的内容。

for(Employ emp : employList){  
    if (!soft.containsKey(emp.getDept())) {
        soft.put(emp.getDept(),new ArrayList());
    }
    soft.get(emp.getDept()).add(emp);
}

顺便说一句,这个循环可以替换为以下 Java 8 代码:

Map<String ,List<Employ>> soft = 
    employList.stream().collect(Collectors.groupingBy(Employ::getDept));

【讨论】:

  • @Ghimire 已修复(假设您希望按部门对 Employ 对象进行分组)
  • 未创建单独的列表
  • @Ghimire 你是什么意思?什么单独的清单?列表在地图内。
猜你喜欢
  • 2021-12-26
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2021-01-14
  • 2021-10-15
相关资源
最近更新 更多