如果在Else条件下动态访问Condition,ArrayList如何创建?

时间:2017-11-10 08:45:51

标签: java arraylist hashmap

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

Getter and 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"));
            }
        }
    }
}

如何实现这一目标?

2 个答案:

答案 0 :(得分:3)

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

必要的方法是

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。

答案 1 :(得分:2)

如果emp.getDept()中存在密钥Map,您可以使用ArrayList获取相应的f1soft.get(emp.getDept())值,并添加您希望的内容。

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));