2022-02-23

How to perform join query using Java stream

I am fetching data from db and storing it in list. Then based on the the id, I want to create new Object. I have already implemented it using old for loop method. I am trying to implement it using java stream. Any help? Thanks.

        List<Employee> emp = new ArrayList<Employee>();
        emp.add(new Employee(1, "Ben", "Glasgow"));
        emp.add(new Employee(2, "Max", "Seattle"));
        emp.add(new Employee(3, "Sam", "Mumbai"));
        emp.add(new Employee(4, "John", "Aukland"));
        emp.add(new Employee(5, "Rob", "Tokyo"));

        List<Department> dpt = new ArrayList<Department>();
        dpt.add(new Department(1, 40000, "tech"));
        dpt.add(new Department(2,  30000, "mgm"));
        dpt.add(new Department(3,  50000, "tech"));
        dpt.add(new Department(4,  30000, "mgm"));


        List<EmpDep> empDep = new ArrayList<EmpDep>();
        
        /**How to do this using stream
        for (int i = 0; i < emp.size(); i++){
            empDep.add(new EmpDep(emp.get(i).getId(), emp.get(i).getName(), dpt.get(i).getSalary()));
        }
        */


1 comment:

  1. It's such a beautiful explanation about the iPhone app developmer demands in 2022. I can understand the concept of how the industry is booming and the languages keep on evolving with time.
    I'm also looking to upgrade my skills but cannot find the right solution without leaving a job. Is it possible to help me out at sealing wood projects side by side to work upon my skills?

    ReplyDelete