简体   繁体   中英

How Can ArrayList Created on if Condition dynamically be accessed on Else Condition?

I have listed the Data, ie name,gender,dept, in a List. I have added the data with different department like QA, Java, Php in the List. Now I have to put the Data with Specific Department in the hashmap (eg all the QA department should be listed on one ArrayList or List dynamically). If I add the new department then it should be Listed in a new ArrayList dynamically, not by hard coding. It is simple logic but I am not able to solve this.

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

Main Class

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

How can this be achieved?

If I understand correctly, you want to group employees by department.

The imperative way would be

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

Or, simpler:

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

But it's must simpler to do that with streams:

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

Note that I choose to rename your types and properties to make everything more readable. There's really no good reason to use Employ instead of Employee, or Dept instead of Department.

If the key emp.getDept() is present in the Map , you can obtain the corresponding ArrayList value with f1soft.get(emp.getDept()) and add whatever you wish to it.

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

BTW, this loop can be replaced with the following Java 8 code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM